回答編集履歴
2
修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
表示だけのことですので、データ
|
1
|
+
表示だけのことですので、リストデータを弄る必要はありません。
|
2
2
|
|
3
3
|
complete と表示したい position のデータを求められたら complete のレイアウトデータを返すようにし、 それ以降のデータを求められたら position-1 の位置の実際のデータを返すだけです。
|
4
4
|
|
@@ -180,7 +180,7 @@
|
|
180
180
|
|
181
181
|
allData.add(todo)
|
182
182
|
|
183
|
-
readAllData.value = allData
|
183
|
+
readAllData.value = allData //LiveData発火
|
184
184
|
|
185
185
|
}
|
186
186
|
|
1
コード追加
test
CHANGED
@@ -1,3 +1,369 @@
|
|
1
1
|
表示だけのことですので、データリストを弄る必要はありません。
|
2
2
|
|
3
3
|
complete と表示したい position のデータを求められたら complete のレイアウトデータを返すようにし、 それ以降のデータを求められたら position-1 の位置の実際のデータを返すだけです。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
----
|
8
|
+
|
9
|
+
Room は関係しなくても良さそうですので、表示部分だけテキトウに作って見ました。
|
10
|
+
|
11
|
+
(単に kotlin も Room も不慣れ(or ほぼ分からない)なだけですが...)
|
12
|
+
|
13
|
+
```kotlin
|
14
|
+
|
15
|
+
package com.teratail.q372341
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
import android.os.Bundle
|
20
|
+
|
21
|
+
import android.view.LayoutInflater
|
22
|
+
|
23
|
+
import android.view.View
|
24
|
+
|
25
|
+
import android.view.ViewGroup
|
26
|
+
|
27
|
+
import android.widget.CheckBox
|
28
|
+
|
29
|
+
import android.widget.TextView
|
30
|
+
|
31
|
+
import androidx.appcompat.app.AppCompatActivity
|
32
|
+
|
33
|
+
import androidx.lifecycle.MutableLiveData
|
34
|
+
|
35
|
+
import androidx.lifecycle.ViewModel
|
36
|
+
|
37
|
+
import androidx.lifecycle.ViewModelProvider
|
38
|
+
|
39
|
+
import androidx.recyclerview.widget.LinearLayoutManager
|
40
|
+
|
41
|
+
import androidx.recyclerview.widget.RecyclerView
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class MainActivity : AppCompatActivity() {
|
46
|
+
|
47
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
48
|
+
|
49
|
+
super.onCreate(savedInstanceState)
|
50
|
+
|
51
|
+
setContentView(R.layout.activity_main)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
val adapter = ToDoAdapter()
|
56
|
+
|
57
|
+
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
|
58
|
+
|
59
|
+
recyclerView.adapter = adapter
|
60
|
+
|
61
|
+
recyclerView.layoutManager = LinearLayoutManager(this)
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
val todoViewModel = ViewModelProvider(this).get(ToDoViewModel::class.java)
|
66
|
+
|
67
|
+
todoViewModel.readAllData.observe(this, { todo -> adapter.setData(todo) })
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
todoViewModel.add(ToDo(1, false, "todo 1"))
|
72
|
+
|
73
|
+
todoViewModel.add(ToDo(2, true, "todo 2"))
|
74
|
+
|
75
|
+
todoViewModel.add(ToDo(3, false, "todo 3"))
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
data class ToDo(
|
84
|
+
|
85
|
+
val id: Int,
|
86
|
+
|
87
|
+
val complete: Boolean,
|
88
|
+
|
89
|
+
val text: String
|
90
|
+
|
91
|
+
)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
class ToDoAdapter: RecyclerView.Adapter<ToDoAdapter.ToDoViewHolder>() {
|
96
|
+
|
97
|
+
private var todoList = emptyList<ToDo>()
|
98
|
+
|
99
|
+
private val layouts = arrayOf(R.layout.recyclerview_item, R.layout.recyclerview_separator)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
class ToDoViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
|
104
|
+
|
105
|
+
val checkBox: CheckBox?
|
106
|
+
|
107
|
+
val textView: TextView?
|
108
|
+
|
109
|
+
init {
|
110
|
+
|
111
|
+
checkBox = itemView.findViewById(R.id.checkBox)
|
112
|
+
|
113
|
+
textView = itemView.findViewById(R.id.textView)
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
override fun getItemViewType(position: Int) = if(position == separateIndex()) 1 else 0
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
126
|
+
|
127
|
+
ToDoViewHolder(LayoutInflater.from(parent.context).inflate(layouts[viewType], parent, false))
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
override fun getItemCount() = todoList.size + if(separateIndex() >= 0) 1 else 0
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
override fun onBindViewHolder(holder: ToDoViewHolder, position: Int) {
|
136
|
+
|
137
|
+
val separateIndex = separateIndex()
|
138
|
+
|
139
|
+
if(position == separateIndex) return //セパレータに表示するデータは無い
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
val pos = position - if(position > separateIndex) 1 else 0
|
144
|
+
|
145
|
+
val todo = todoList[pos]
|
146
|
+
|
147
|
+
holder.checkBox?.isChecked = todo.complete
|
148
|
+
|
149
|
+
holder.textView?.text = todo.text
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
fun setData(todoList: List<ToDo>) {
|
156
|
+
|
157
|
+
this.todoList = todoList.sortedWith(compareBy<ToDo> { it.complete }.thenBy { it.id })
|
158
|
+
|
159
|
+
notifyDataSetChanged()
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
private fun separateIndex() = todoList.indexOfFirst { it.complete }
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
class ToDoViewModel() : ViewModel() {
|
172
|
+
|
173
|
+
private var allData : MutableList<ToDo> = mutableListOf()
|
174
|
+
|
175
|
+
val readAllData = MutableLiveData<List<ToDo>>(allData)
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
fun add(todo: ToDo) {
|
180
|
+
|
181
|
+
allData.add(todo)
|
182
|
+
|
183
|
+
readAllData.value = allData
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
res/layout/activity_main.xml
|
192
|
+
|
193
|
+
```xml
|
194
|
+
|
195
|
+
<?xml version="1.0" encoding="utf-8"?>
|
196
|
+
|
197
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
198
|
+
|
199
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
200
|
+
|
201
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
202
|
+
|
203
|
+
xmlns:tools="http://schemas.android.com/tools"
|
204
|
+
|
205
|
+
android:layout_width="match_parent"
|
206
|
+
|
207
|
+
android:layout_height="match_parent"
|
208
|
+
|
209
|
+
tools:context=".MainActivity">
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
<androidx.recyclerview.widget.RecyclerView
|
214
|
+
|
215
|
+
android:id="@+id/recyclerview"
|
216
|
+
|
217
|
+
android:layout_width="0dp"
|
218
|
+
|
219
|
+
android:layout_height="0dp"
|
220
|
+
|
221
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
222
|
+
|
223
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
224
|
+
|
225
|
+
app:layout_constraintRight_toRightOf="parent"
|
226
|
+
|
227
|
+
app:layout_constraintTop_toTopOf="parent" />
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
res/layout/recyclerview_item.xml
|
236
|
+
|
237
|
+
```xml
|
238
|
+
|
239
|
+
<?xml version="1.0" encoding="utf-8"?>
|
240
|
+
|
241
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
242
|
+
|
243
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
244
|
+
|
245
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
246
|
+
|
247
|
+
android:layout_width="match_parent"
|
248
|
+
|
249
|
+
android:layout_height="wrap_content">
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
<CheckBox
|
254
|
+
|
255
|
+
android:id="@+id/checkBox"
|
256
|
+
|
257
|
+
android:layout_width="wrap_content"
|
258
|
+
|
259
|
+
android:layout_height="wrap_content"
|
260
|
+
|
261
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
262
|
+
|
263
|
+
app:layout_constraintTop_toTopOf="parent" />
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
<TextView
|
268
|
+
|
269
|
+
android:id="@+id/textView"
|
270
|
+
|
271
|
+
android:layout_width="0dp"
|
272
|
+
|
273
|
+
android:layout_height="wrap_content"
|
274
|
+
|
275
|
+
app:layout_constraintLeft_toRightOf="@id/checkBox"
|
276
|
+
|
277
|
+
app:layout_constraintRight_toRightOf="parent"
|
278
|
+
|
279
|
+
app:layout_constraintBaseline_toBaselineOf="@id/checkBox" />
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
res/layout/recyclerview_separator.xml
|
288
|
+
|
289
|
+
```xml
|
290
|
+
|
291
|
+
<?xml version="1.0" encoding="utf-8"?>
|
292
|
+
|
293
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
294
|
+
|
295
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
296
|
+
|
297
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
298
|
+
|
299
|
+
android:layout_width="match_parent"
|
300
|
+
|
301
|
+
android:layout_height="wrap_content">
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
<TextView
|
306
|
+
|
307
|
+
android:layout_width="0dp"
|
308
|
+
|
309
|
+
android:layout_height="wrap_content"
|
310
|
+
|
311
|
+
android:text="Complete"
|
312
|
+
|
313
|
+
android:layout_marginTop="5dp"
|
314
|
+
|
315
|
+
android:layout_marginBottom="10dp"
|
316
|
+
|
317
|
+
android:paddingLeft="5dp"
|
318
|
+
|
319
|
+
android:background="@drawable/upperline"
|
320
|
+
|
321
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
322
|
+
|
323
|
+
app:layout_constraintRight_toRightOf="parent"
|
324
|
+
|
325
|
+
app:layout_constraintTop_toTopOf="parent" />
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
330
|
+
|
331
|
+
```
|
332
|
+
|
333
|
+
res/drawable/upperline.xml
|
334
|
+
|
335
|
+
```xml
|
336
|
+
|
337
|
+
<?xml version="1.0" encoding="utf-8"?>
|
338
|
+
|
339
|
+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
<item>
|
344
|
+
|
345
|
+
<shape android:shape="rectangle">
|
346
|
+
|
347
|
+
<solid android:color="#FFFFFF"/>
|
348
|
+
|
349
|
+
<stroke android:width="1dp" android:color="#AAAAAA" />
|
350
|
+
|
351
|
+
</shape>
|
352
|
+
|
353
|
+
</item>
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
<item android:top="1dp">
|
358
|
+
|
359
|
+
<shape android:shape="rectangle">
|
360
|
+
|
361
|
+
<solid android:color="#FFFFFF"/>
|
362
|
+
|
363
|
+
</shape>
|
364
|
+
|
365
|
+
</item>
|
366
|
+
|
367
|
+
</layer-list>
|
368
|
+
|
369
|
+
```
|