質問編集履歴
2
タイトルを直しました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
RecyclerView
|
1
|
+
RecyclerViewを使ったリストのレイアウトについて
|
test
CHANGED
File without changes
|
1
使用したコードとエラーを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,12 +12,504 @@
|
|
12
12
|
|
13
13
|
### 該当のソースコード
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
レイアウト
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
<?xml version="1.0" encoding="utf-8"?>
|
20
|
+
|
21
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
22
|
+
|
23
|
+
android:layout_width="match_parent"
|
24
|
+
|
25
|
+
android:layout_height="match_parent">
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
<TextView
|
30
|
+
|
31
|
+
android:layout_width="match_parent"
|
32
|
+
|
33
|
+
android:layout_height="wrap_content"
|
34
|
+
|
35
|
+
android:padding="3dp"
|
36
|
+
|
37
|
+
android:background="#FF0000"
|
38
|
+
|
39
|
+
android:text="New Text"
|
40
|
+
|
41
|
+
android:id="@+id/textView1" /> <!-- textViewn -->
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
</LinearLayout>
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```MyAdapter
|
52
|
+
|
53
|
+
package com.example.myapplication;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
import android.content.Context;
|
58
|
+
|
59
|
+
import android.support.v7.widget.RecyclerView;
|
60
|
+
|
61
|
+
import android.view.LayoutInflater;
|
62
|
+
|
63
|
+
import android.view.View;
|
64
|
+
|
65
|
+
import android.view.ViewGroup;
|
66
|
+
|
67
|
+
import android.widget.TextView;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
import java.util.List;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
private final LayoutInflater inflater;
|
80
|
+
|
81
|
+
private final List<SampleItem> data;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
public MyAdapter(Context context, List<SampleItem> data){
|
86
|
+
|
87
|
+
this.inflater = LayoutInflater.from(context);
|
88
|
+
|
89
|
+
this.data = data;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
@Override
|
96
|
+
|
97
|
+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
|
98
|
+
|
99
|
+
switch(viewType){
|
100
|
+
|
101
|
+
case 1:
|
102
|
+
|
103
|
+
return new Sample1ItemViewHolder(inflater.inflate(Sample1ItemViewHolder.LAYOUT_ID, parent, false));
|
104
|
+
|
105
|
+
case 2:
|
106
|
+
|
107
|
+
return new Sample2ItemViewHolder(inflater.inflate(Sample2ItemViewHolder.LAYOUT_ID, parent, false));
|
108
|
+
|
109
|
+
case 3:
|
110
|
+
|
111
|
+
return new Sample3ItemViewHolder(inflater.inflate(Sample3ItemViewHolder.LAYOUT_ID, parent, false));
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
return null;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@Override
|
122
|
+
|
123
|
+
public void onBindViewHolder(ViewHolder holder, int position){
|
124
|
+
|
125
|
+
switch(getItemViewType(position)){
|
126
|
+
|
127
|
+
case 1:
|
128
|
+
|
129
|
+
((Sample1ItemViewHolder)holder).onBindItemViewHolder(data.get(position));
|
130
|
+
|
131
|
+
break;
|
132
|
+
|
133
|
+
case 2:
|
134
|
+
|
135
|
+
((Sample2ItemViewHolder)holder).onBindItemViewHolder(data.get(position));
|
136
|
+
|
137
|
+
break;
|
138
|
+
|
139
|
+
case 3:
|
140
|
+
|
141
|
+
((Sample3ItemViewHolder)holder).onBindItemViewHolder(data.get(position));
|
142
|
+
|
143
|
+
break;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
public int getItemViewType(int position){
|
152
|
+
|
153
|
+
return data.get(position).getCategory();
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
@Override
|
160
|
+
|
161
|
+
public int getItemCount(){
|
162
|
+
|
163
|
+
return data.size();
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
public class ViewHolder extends RecyclerView.ViewHolder{
|
170
|
+
|
171
|
+
public ViewHolder(View itemView){
|
172
|
+
|
173
|
+
super(itemView);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public class Sample1ItemViewHolder extends ViewHolder{
|
182
|
+
|
183
|
+
public static final int LAYOUT_ID = R.layout.sample_1_item_view;
|
184
|
+
|
185
|
+
public final TextView textView;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
public Sample1ItemViewHolder(View itemView){
|
190
|
+
|
191
|
+
super(itemView);
|
192
|
+
|
193
|
+
textView = itemView.findViewById(R.id.textView1);
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public void onBindItemViewHolder(SampleItem data){
|
200
|
+
|
201
|
+
this.textView.setText(data.getText());
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
public class Sample2ItemViewHolder extends ViewHolder{
|
210
|
+
|
211
|
+
public static final int LAYOUT_ID = R.layout.sample_2_item_view;
|
212
|
+
|
213
|
+
public final TextView textView;
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
public Sample2ItemViewHolder(View itemView){
|
218
|
+
|
219
|
+
super(itemView);
|
220
|
+
|
221
|
+
textView = itemView.findViewById(R.id.textView2);
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
public void onBindItemViewHolder(SampleItem data){
|
228
|
+
|
229
|
+
this.textView.setText(data.getText());
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public class Sample3ItemViewHolder extends ViewHolder{
|
238
|
+
|
239
|
+
public static final int LAYOUT_ID = R.layout.sample_3_item_view;
|
240
|
+
|
241
|
+
public final TextView textView;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
public Sample3ItemViewHolder(View itemView){
|
246
|
+
|
247
|
+
super(itemView);
|
248
|
+
|
249
|
+
textView = itemView.findViewById(R.id.textView3);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
public void onBindItemViewHolder(SampleItem data){
|
256
|
+
|
257
|
+
this.textView.setText(data.getText());
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
```SampleItem
|
270
|
+
|
271
|
+
package com.example.myapplication;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
public class SampleItem {
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
private String text;
|
280
|
+
|
281
|
+
private int category;
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
public SampleItem(int category, String text){
|
286
|
+
|
287
|
+
this.category = category;
|
288
|
+
|
289
|
+
this.text = text;
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
public String getText(){
|
296
|
+
|
297
|
+
return this.text;
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
public int getCategory(){
|
304
|
+
|
305
|
+
return this.category;
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
```
|
312
|
+
|
313
|
+
```MainActivity
|
314
|
+
|
315
|
+
package com.example.myapplication;
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
import android.support.v7.app.AppCompatActivity;
|
320
|
+
|
321
|
+
import android.support.v7.widget.LinearLayoutManager;
|
322
|
+
|
323
|
+
import android.support.v7.widget.RecyclerView;
|
324
|
+
|
325
|
+
import android.os.Bundle;
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
import java.util.ArrayList;
|
330
|
+
|
331
|
+
import java.util.List;
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
public class MainActivity extends AppCompatActivity {
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
@Override
|
342
|
+
|
343
|
+
protected void onCreate(Bundle savedInstanceState) {
|
344
|
+
|
345
|
+
super.onCreate(savedInstanceState);
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
RecyclerView recyclerView = findViewById(R.id.recycler_view);
|
350
|
+
|
351
|
+
recyclerView.setHasFixedSize(true);
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
356
|
+
|
357
|
+
recyclerView.setLayoutManager(layoutManager);
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
List<SampleItem> data = new ArrayList<>();
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
SampleItem item = null;
|
366
|
+
|
367
|
+
for (int i = 0; i < 30; i++){
|
368
|
+
|
369
|
+
switch(i % 10) {
|
370
|
+
|
371
|
+
case 0:
|
372
|
+
|
373
|
+
item = new SampleItem(1, "Data" + i);
|
374
|
+
|
375
|
+
break;
|
376
|
+
|
377
|
+
case 1:
|
378
|
+
|
379
|
+
item = new SampleItem(1, "Data" + i);
|
380
|
+
|
381
|
+
break;
|
382
|
+
|
383
|
+
case 2:
|
384
|
+
|
385
|
+
item = new SampleItem(2, "Data" + i);
|
386
|
+
|
387
|
+
break;
|
388
|
+
|
389
|
+
case 3:
|
390
|
+
|
391
|
+
item = new SampleItem(3, "Data" + i);
|
392
|
+
|
393
|
+
break;
|
394
|
+
|
395
|
+
case 4:
|
396
|
+
|
397
|
+
item = new SampleItem(2, "Data" + i);
|
398
|
+
|
399
|
+
break;
|
400
|
+
|
401
|
+
case 5:
|
402
|
+
|
403
|
+
item = new SampleItem(1, "Data" + i);
|
404
|
+
|
405
|
+
break;
|
406
|
+
|
407
|
+
case 6:
|
408
|
+
|
409
|
+
item = new SampleItem(2, "Data" + i);
|
410
|
+
|
411
|
+
break;
|
412
|
+
|
413
|
+
case 7:
|
414
|
+
|
415
|
+
item = new SampleItem(2, "Data" + i);
|
416
|
+
|
417
|
+
break;
|
418
|
+
|
419
|
+
case 8:
|
420
|
+
|
421
|
+
item = new SampleItem(3, "Data" + i);
|
422
|
+
|
423
|
+
break;
|
424
|
+
|
425
|
+
case 9:
|
426
|
+
|
427
|
+
item = new SampleItem(1, "Data" + i);
|
428
|
+
|
429
|
+
break;
|
430
|
+
|
431
|
+
}
|
432
|
+
|
433
|
+
data.add(item);
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
recyclerView.setAdapter(new MyAdapter(this, data));
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
```
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
[RecycleViewで行ごとに表示を切り替える](https://woshidan.hatenablog.com/entry/2015/11/02/083000)
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
上のよさげなコードを試してみたのですが、アプリが停止します。
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
Logcatのエラー
|
458
|
+
|
459
|
+
> E/AndroidRuntime: FATAL EXCEPTION: main
|
460
|
+
|
461
|
+
Process: com.example.myapplication, PID: 12165
|
462
|
+
|
463
|
+
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
|
464
|
+
|
465
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
|
466
|
+
|
467
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
|
468
|
+
|
469
|
+
at android.app.ActivityThread.-wrap12(ActivityThread.java)
|
470
|
+
|
471
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
|
472
|
+
|
473
|
+
at android.os.Handler.dispatchMessage(Handler.java:102)
|
474
|
+
|
475
|
+
at android.os.Looper.loop(Looper.java:154)
|
476
|
+
|
477
|
+
at android.app.ActivityThread.main(ActivityThread.java:6119)
|
478
|
+
|
479
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
480
|
+
|
481
|
+
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
|
482
|
+
|
483
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
|
484
|
+
|
485
|
+
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
|
486
|
+
|
487
|
+
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:19)
|
488
|
+
|
489
|
+
at android.app.Activity.performCreate(Activity.java:6679)
|
490
|
+
|
491
|
+
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
|
492
|
+
|
493
|
+
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
|
494
|
+
|
495
|
+
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
|
496
|
+
|
497
|
+
at android.app.ActivityThread.-wrap12(ActivityThread.java)
|
498
|
+
|
499
|
+
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
|
500
|
+
|
501
|
+
at android.os.Handler.dispatchMessage(Handler.java:102)
|
502
|
+
|
503
|
+
at android.os.Looper.loop(Looper.java:154)
|
504
|
+
|
505
|
+
at android.app.ActivityThread.main(ActivityThread.java:6119)
|
506
|
+
|
507
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
508
|
+
|
509
|
+
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
|
510
|
+
|
511
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
お願いします。
|