質問編集履歴

3

内容の追記

2020/01/21 08:45

投稿

Haruto513
Haruto513

スコア52

test CHANGED
File without changes
test CHANGED
@@ -8,93 +8,149 @@
8
8
 
9
9
 
10
10
 
11
- ボタンタップで、JSONをダウンロードしてからフラグメントを表示しています。
11
+ ボタンタップで、JSONをダウンロードしてからフラグメントを表示しています。
12
+
13
+
14
+
15
+ //質問の字数制限に達したので一部内容を消去しました。
16
+
17
+
18
+
19
+ ②このようにしてRecyclerViewを表示しています。
12
20
 
13
21
  ```Kotlin
14
22
 
15
23
 
16
24
 
17
-
25
+ private var _download : MutableList<JSONObject>? = null
18
-
26
+
19
- class MainActivity : AppCompatActivity() {
27
+ private var _list : List<JSONObject>? = null
20
-
21
-
22
-
23
-
24
-
28
+
29
+
30
+
31
+
32
+
25
- override fun onCreate(savedInstanceState: Bundle?) {
33
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
34
+
35
+
36
+
26
-
37
+ val extra = arguments
38
+
39
+ val downloadStr = extra?.getString("JsonData")
40
+
41
+ val array = JSONArray(downloadStr)
42
+
27
- super.onCreate(savedInstanceState)
43
+ _download = array.toMutableList()
28
-
44
+
29
- setContentView(R.layout.activity_main)
45
+ val firstView = _download!!.take(10)
30
-
31
- }
46
+
32
-
33
-
34
-
35
- fun StartDownload(view: View) {
47
+ _list = _download!!.drop(10)
48
+
49
+
50
+
36
-
51
+ val view = inflater.inflate(R.layout.listview_fragment, container, false)
52
+
53
+
54
+
37
- val preText = findViewById<TextView>(R.id.preText)
55
+ val rvList = view.findViewById<RecyclerView>(R.id.rvDrinkList)
56
+
38
-
57
+ val layout = LinearLayoutManager(activity)
58
+
39
- preText.isVisible = false
59
+ rvList.layoutManager = layout
40
-
41
- val progress = findViewById<ProgressBar>(R.id.progress)
60
+
42
-
61
+
62
+
43
- progress.isVisible = true
63
+ val dataList = firstView
44
-
45
-
46
-
64
+
65
+
66
+
47
- val start = DownloadJsonData()
67
+ val adapter = RecyclerViewAdapter(dataList)
68
+
48
-
69
+ rvList.adapter = adapter
70
+
71
+
72
+
73
+ rvList.addOnScrollListener(scrollListener())
74
+
75
+ val decorator = DividerItemDecoration(activity, layout.orientation)
76
+
49
- start.execute("")
77
+ rvList.addItemDecoration(decorator)
78
+
79
+
80
+
81
+ return view
50
82
 
51
83
  }
52
84
 
53
85
 
54
86
 
87
+ //略//
88
+
55
- private inner class DownloadJsonData(): AsyncTask<String, String, String>() {
89
+ private inner class RecyclerViewAdapter(private val _listData: List<JSONObject>): RecyclerView.Adapter<RecyclerViewHolder>() {
56
-
90
+
57
- override fun doInBackground(vararg params: String?): String {
91
+ override fun getItemCount(): Int {
58
-
92
+
59
- val result = downloadTask()
93
+ LAST_POSITION = _listData.size
60
-
94
+
61
- return result
95
+ return _listData.size
62
-
96
+
63
- }
97
+ }
64
-
65
-
66
-
98
+
67
- override fun onPostExecute(result: String?) {
99
+ override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
68
-
69
-
70
-
100
+
71
- val bundle = Bundle()
101
+ val list = _listData[position]
72
-
102
+
73
- bundle.putString("JsonData", result)
103
+ holder.tvname.text = list["name"].toString()
74
-
75
-
76
-
77
- val transaction = supportFragmentManager.beginTransaction()
104
+
78
-
79
- val recyclerViewFragment = RecyclerViewFragment()
80
-
81
- recyclerViewFragment.arguments = bundle
82
-
83
- transaction.replace(R.id.recyclerViewFrame, recyclerViewFragment)
84
-
85
- transaction.commit()
86
-
87
-
88
-
89
- val progress = findViewById<ProgressBar>(R.id.progress)
105
+ holder.tvkind.text = list["kind"].toString()
90
-
91
- progress.isVisible = false
106
+
92
-
93
- }
107
+ }
108
+
109
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
110
+
111
+ val holder = layoutInflater.inflate(R.layout.row, parent, false)
112
+
113
+ return RecyclerViewHolder(holder)
114
+
115
+ }
116
+
117
+
94
118
 
95
119
  }
96
120
 
97
-
121
+ //略//
122
+
123
+ ```
124
+
125
+
126
+
127
+ ③ここで、最下部までスクロールしたことを検知しています。
128
+
129
+
130
+
131
+ ```Kotlin
132
+
133
+
134
+
135
+ private inner class scrollListener: RecyclerView.OnScrollListener() {
136
+
137
+ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
138
+
139
+ super.onScrollStateChanged(recyclerView, newState)
140
+
141
+
142
+
143
+ if (!recyclerView.canScrollVertically(1)) {
144
+
145
+
146
+
147
+ Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
148
+
149
+
150
+
151
+ }
152
+
153
+ }
98
154
 
99
155
  }
100
156
 
@@ -102,17 +158,17 @@
102
158
 
103
159
 
104
160
 
105
-
106
-
107
-
108
-
109
- ```
161
+ ```
162
+
163
+
164
+
110
-
165
+ ### 試したこと
111
-
112
-
113
-
114
-
166
+
167
+
168
+
115
- ②このようにしRecyclerViewを表示ています
169
+ 読めないなりに英語ドキュメントやJavaでの情報を見てみて、以下のようにいくつか書いてみまたがRecyclerViewは更新されませんで
170
+
171
+
116
172
 
117
173
  ```Kotlin
118
174
 
@@ -120,15 +176,281 @@
120
176
 
121
177
 
122
178
 
179
+ private inner class scrollListener: RecyclerView.OnScrollListener() {
180
+
181
+ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
182
+
183
+ super.onScrollStateChanged(recyclerView, newState)
184
+
185
+
186
+
187
+ if (!recyclerView.canScrollVertically(1)) {
188
+
189
+
190
+
191
+ Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
192
+
193
+
194
+
195
+ RecyclerViewAdapter(list).notifyItemInserted(0)
196
+
197
+
198
+
199
+ }
200
+
201
+ }
202
+
203
+ }
204
+
205
+
206
+
207
+
208
+
209
+ ```
210
+
211
+
212
+
213
+ ```Kotlin
214
+
215
+
216
+
217
+ private inner class scrollListener: RecyclerView.OnScrollListener() {
218
+
219
+ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
220
+
221
+ super.onScrollStateChanged(recyclerView, newState)
222
+
223
+
224
+
225
+ if (!recyclerView.canScrollVertically(1)) {
226
+
227
+
228
+
229
+ Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
230
+
231
+
232
+
233
+ RecyclerViewAdapter(list).notifyItemInserted(0)
234
+
235
+ recyclerView.adapter!!.notifyDataSetChanged()
236
+
237
+
238
+
239
+ }
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+ ```
250
+
251
+
252
+
253
+ どのように書くのが正しいやり方なのでしょうか。
254
+
255
+ どなたか助言を頂けると助かります。
256
+
257
+
258
+
259
+ ### 追記
260
+
261
+
262
+
263
+ こちらのページを参考に、記述を追加しましたがエラーが出ています。
264
+
265
+ [http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner](http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner)
266
+
267
+
268
+
269
+
270
+
271
+ ```Kotlin
272
+
273
+
274
+
275
+ private inner class RecyclerViewAdapter(
276
+
277
+ private val _listData: MutableList<JSONObject>)
278
+
279
+ : RecyclerView.Adapter<RecyclerViewHolder>()
280
+
281
+ {
282
+
283
+ override fun getItemCount(): Int
284
+
285
+ {
286
+
287
+ LAST_POSITION = _listData.size
288
+
289
+ return _listData.size
290
+
291
+ }
292
+
293
+ fun addItem(item: JSONObject) {
294
+
295
+ _listData.add(item)
296
+
297
+ notifyDataSetChanged()
298
+
299
+ }
300
+
301
+ override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int)
302
+
303
+ {
304
+
305
+ val list = _listData[position]
306
+
307
+ holder.tvname.text = list["name"].toString()
308
+
309
+ holder.tvkind.text = list["kind"].toString()
310
+
311
+ }
312
+
313
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder
314
+
315
+ {
316
+
317
+ val holder = layoutInflater.inflate(R.layout.row, parent, false)
318
+
319
+ return RecyclerViewHolder(holder)
320
+
321
+ }
322
+
323
+
324
+
325
+ }
326
+
327
+
328
+
329
+ private inner class scrollListener: RecyclerView.OnScrollListener() {
330
+
331
+ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
332
+
333
+ super.onScrollStateChanged(recyclerView, newState)
334
+
335
+
336
+
337
+ if (!recyclerView.canScrollVertically(1)) {
338
+
339
+
340
+
341
+ val addlist = _list?.take(10) as MutableList<JSONObject>
342
+
343
+ val firstList = _list as MutableList<JSONObject>
344
+
345
+ _list = _list!!.drop(10)
346
+
347
+
348
+
349
+ Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
350
+
351
+
352
+
353
+ for (index in 1..addlist.size) {
354
+
355
+ println(addlist[index - 1])
356
+
357
+ RecyclerViewAdapter(firstList).addItem(addlist[index - 1])
358
+
359
+ }
360
+
361
+ .
362
+
363
+ .
364
+
365
+ .
366
+
367
+
368
+
369
+
370
+
371
+ ```
372
+
373
+
374
+
375
+ リサイクラービューを設定するメンバクラスの中に、
376
+
377
+
378
+
379
+ ```kotlin
380
+
381
+
382
+
383
+ fun addItem(item: JSONObject) {
384
+
385
+ _listData.add(item)
386
+
387
+ notifyDataSetChanged()
388
+
389
+ }
390
+
391
+ ```
392
+
393
+
394
+
395
+ を追加しました。
396
+
397
+
398
+
399
+ そして上記の内容で実行すると、エラーが出てきました。
400
+
401
+
402
+
403
+ ```logcat
404
+
405
+
406
+
407
+ java.lang.ClassCastException: kotlin.collections.EmptyList cannot be cast to kotlin.collections.MutableList
408
+
409
+ at jp.wings.nikkeibp.sampleapp.RecyclerViewFragment$scrollListener.onScrollStateChanged(RecyclerViewFragment.kt:100)
410
+
411
+
412
+
413
+ ```
414
+
415
+
416
+
417
+ もう少し調べてみようと思います。
418
+
419
+
420
+
421
+ ### 追記2
422
+
423
+
424
+
425
+ その後も少しずつ修正を加えていき、現在の記述は以下のようになっています。
426
+
427
+
428
+
429
+ ```Kotlin
430
+
431
+
432
+
433
+
434
+
435
+ class RecyclerViewFragment : Fragment() {
436
+
437
+
438
+
123
- private var _download : MutableList<JSONObject>? = null
439
+ private var _download: MutableList<JSONObject>? = null
440
+
441
+
442
+
443
+
444
+
124
-
445
+ override fun onCreateView(
446
+
447
+ inflater: LayoutInflater,
448
+
449
+ container: ViewGroup?,
450
+
125
- private var _list : List<JSONObject>? = null
451
+ savedInstanceState: Bundle?
126
-
127
-
128
-
129
-
130
-
452
+
131
- override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
453
+ ): View? {
132
454
 
133
455
 
134
456
 
@@ -140,9 +462,15 @@
140
462
 
141
463
  _download = array.toMutableList()
142
464
 
465
+
466
+
143
- val firstView = _download!!.take(10)
467
+ var datalist = mutableListOf<JSONObject>()
468
+
144
-
469
+ for (index in 1..10) {
470
+
145
- _list = _download!!.drop(10)
471
+ datalist.add(_download!![index - 1])
472
+
473
+ }
146
474
 
147
475
 
148
476
 
@@ -158,11 +486,7 @@
158
486
 
159
487
 
160
488
 
161
- val dataList = firstView
162
-
163
-
164
-
165
- val adapter = RecyclerViewAdapter(dataList)
489
+ val adapter = RecyclerViewAdapter(datalist)
166
490
 
167
491
  rvList.adapter = adapter
168
492
 
@@ -184,9 +508,11 @@
184
508
 
185
509
  private inner class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
186
510
 
187
- var tvname : TextView
511
+ var tvname: TextView
188
-
512
+
189
- var tvkind :TextView
513
+ var tvkind: TextView
514
+
515
+
190
516
 
191
517
  init {
192
518
 
@@ -200,16 +526,30 @@
200
526
 
201
527
 
202
528
 
529
+ private inner class RecyclerViewAdapter(
530
+
531
+ private val _listData: MutableList<JSONObject>
532
+
203
- private inner class RecyclerViewAdapter(private val _listData: List<JSONObject>): RecyclerView.Adapter<RecyclerViewHolder>() {
533
+ ) : RecyclerView.Adapter<RecyclerViewHolder>() {
204
534
 
205
535
  override fun getItemCount(): Int {
206
536
 
207
- LAST_POSITION = _listData.size
208
-
209
537
  return _listData.size
210
538
 
211
539
  }
212
540
 
541
+
542
+
543
+ fun addItem(item: JSONObject) {
544
+
545
+ _listData.add(item)
546
+
547
+ notifyDataSetChanged()
548
+
549
+ }
550
+
551
+
552
+
213
553
  override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
214
554
 
215
555
  val list = _listData[position]
@@ -220,6 +560,8 @@
220
560
 
221
561
  }
222
562
 
563
+
564
+
223
565
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
224
566
 
225
567
  val holder = layoutInflater.inflate(R.layout.row, parent, false)
@@ -234,29 +576,13 @@
234
576
 
235
577
 
236
578
 
237
- fun JSONArray.toMutableList() : MutableList<JSONObject> = MutableList(length(), this::getJSONObject)
579
+ fun JSONArray.toMutableList(): MutableList<JSONObject> =
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
- ```
580
+
248
-
249
-
250
-
251
- ③ここで、最下部までスクロールしたことを検知しています。
581
+ MutableList(length(), this::getJSONObject)
252
-
253
-
254
-
255
- ```Kotlin
582
+
256
-
257
-
258
-
583
+
584
+
259
- private inner class scrollListener: RecyclerView.OnScrollListener() {
585
+ private inner class scrollListener : RecyclerView.OnScrollListener() {
260
586
 
261
587
  override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
262
588
 
@@ -266,261 +592,39 @@
266
592
 
267
593
  if (!recyclerView.canScrollVertically(1)) {
268
594
 
269
-
595
+ var datalist = mutableListOf<JSONObject>()
596
+
597
+ for (index in 1..10) {
598
+
599
+ datalist.add(_download!![index - 1])
600
+
601
+ }
602
+
603
+ for (index in datalist.size..datalist.size + 10) {
604
+
605
+ val recyclerView = RecyclerViewAdapter(datalist)
606
+
607
+ recyclerView.addItem(_download!![index])
608
+
609
+ }
270
610
 
271
611
  Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
272
612
 
273
-
274
-
275
613
  }
276
614
 
277
615
  }
278
616
 
617
+ }
618
+
279
619
  }
280
620
 
281
621
 
282
622
 
283
-
284
-
285
- ```
623
+ ```
286
-
287
-
288
-
289
- ### 試したこと
624
+
290
-
291
-
292
-
625
+
626
+
293
- 読めいなに英語のドキュメントやJavaの情報を見てみて、以下のようにいつか書いてましたがRecyclerView更新されませんでした。
627
+ クラッシュするようエラーはあませんが、最下部まスクロールしscrollListenerが動くと、以下のようなログが出てるので、リサイクラービューに変化起こりませんでした。
294
-
295
-
296
-
297
- ```Kotlin
298
-
299
-
300
-
301
-
302
-
303
- private inner class scrollListener: RecyclerView.OnScrollListener() {
304
-
305
- override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
306
-
307
- super.onScrollStateChanged(recyclerView, newState)
308
-
309
-
310
-
311
- if (!recyclerView.canScrollVertically(1)) {
312
-
313
-
314
-
315
- Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
316
-
317
-
318
-
319
- RecyclerViewAdapter(list).notifyItemInserted(0)
320
-
321
-
322
-
323
- }
324
-
325
- }
326
-
327
- }
328
-
329
-
330
-
331
-
332
-
333
- ```
334
-
335
-
336
-
337
- ```Kotlin
338
-
339
-
340
-
341
- private inner class scrollListener: RecyclerView.OnScrollListener() {
342
-
343
- override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
344
-
345
- super.onScrollStateChanged(recyclerView, newState)
346
-
347
-
348
-
349
- if (!recyclerView.canScrollVertically(1)) {
350
-
351
-
352
-
353
- Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
354
-
355
-
356
-
357
- RecyclerViewAdapter(list).notifyItemInserted(0)
358
-
359
- recyclerView.adapter!!.notifyDataSetChanged()
360
-
361
-
362
-
363
- }
364
-
365
- }
366
-
367
- }
368
-
369
-
370
-
371
-
372
-
373
- ```
374
-
375
-
376
-
377
- どのように書くのが正しいやり方なのでしょうか。
378
-
379
- どなたか助言を頂けると助かります。
380
-
381
-
382
-
383
- ### 追記
384
-
385
-
386
-
387
- こちらのページを参考に、記述を追加しましたがエラーが出ています。
388
-
389
- [http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner](http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner)
390
-
391
-
392
-
393
-
394
-
395
- ```Kotlin
396
-
397
-
398
-
399
- private inner class RecyclerViewAdapter(
400
-
401
- private val _listData: MutableList<JSONObject>)
402
-
403
- : RecyclerView.Adapter<RecyclerViewHolder>()
404
-
405
- {
406
-
407
- override fun getItemCount(): Int
408
-
409
- {
410
-
411
- LAST_POSITION = _listData.size
412
-
413
- return _listData.size
414
-
415
- }
416
-
417
- fun addItem(item: JSONObject) {
418
-
419
- _listData.add(item)
420
-
421
- notifyDataSetChanged()
422
-
423
- }
424
-
425
- override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int)
426
-
427
- {
428
-
429
- val list = _listData[position]
430
-
431
- holder.tvname.text = list["name"].toString()
432
-
433
- holder.tvkind.text = list["kind"].toString()
434
-
435
- }
436
-
437
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder
438
-
439
- {
440
-
441
- val holder = layoutInflater.inflate(R.layout.row, parent, false)
442
-
443
- return RecyclerViewHolder(holder)
444
-
445
- }
446
-
447
-
448
-
449
- }
450
-
451
-
452
-
453
- private inner class scrollListener: RecyclerView.OnScrollListener() {
454
-
455
- override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
456
-
457
- super.onScrollStateChanged(recyclerView, newState)
458
-
459
-
460
-
461
- if (!recyclerView.canScrollVertically(1)) {
462
-
463
-
464
-
465
- val addlist = _list?.take(10) as MutableList<JSONObject>
466
-
467
- val firstList = _list as MutableList<JSONObject>
468
-
469
- _list = _list!!.drop(10)
470
-
471
-
472
-
473
- Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
474
-
475
-
476
-
477
- for (index in 1..addlist.size) {
478
-
479
- println(addlist[index - 1])
480
-
481
- RecyclerViewAdapter(firstList).addItem(addlist[index - 1])
482
-
483
- }
484
-
485
- .
486
-
487
- .
488
-
489
- .
490
-
491
-
492
-
493
-
494
-
495
- ```
496
-
497
-
498
-
499
- リサイクラービューを設定するメンバクラスの中に、
500
-
501
-
502
-
503
- ```kotlin
504
-
505
-
506
-
507
- fun addItem(item: JSONObject) {
508
-
509
- _listData.add(item)
510
-
511
- notifyDataSetChanged()
512
-
513
- }
514
-
515
- ```
516
-
517
-
518
-
519
- を追加しました。
520
-
521
-
522
-
523
- そして上記の内容で実行すると、エラーが出てきました。
524
628
 
525
629
 
526
630
 
@@ -528,22 +632,16 @@
528
632
 
529
633
 
530
634
 
531
- java.lang.ClassCastException: kotlin.collections.EmptyList cannot be cast to kotlin.collections.MutableList
532
-
533
- at jp.wings.nikkeibp.sampleapp.RecyclerViewFragment$scrollListener.onScrollStateChanged(RecyclerViewFragment.kt:100)
635
+ 2020-01-21 17:07:50.352 7484-7511/jp.wings.nikkeibp.sampleapp E/EGL_emulation: tid 7511: eglSurfaceAttrib(1354): error 0x3009 (EGL_BAD_MATCH)
534
-
636
+
535
- at androidx.recyclerview.widget.RecyclerView.dispatchOnScrollStateChanged(RecyclerView.java:4998)
637
+ 2020-01-21 17:07:50.352 7484-7511/jp.wings.nikkeibp.sampleapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2c46ac0, error=EGL_BAD_MATCH
536
-
537
- at androidx.recyclerview.widget.RecyclerView.setScrollState(RecyclerView.java:1545)
638
+
538
-
539
- at androidx.recyclerview.widget.RecyclerView.onTouchEvent(RecyclerView.java:3212)
639
+
540
-
541
- ................
640
+
542
-
543
-
544
-
641
+
642
+
545
- ```
643
+ ```
546
-
547
-
548
-
644
+
645
+
646
+
549
- もう少し調べみよういます。
647
+ 何か抜けいるころがあるのか、わかる方がたら教えてくださると助かります。

2

内容の修正

2020/01/21 08:45

投稿

Haruto513
Haruto513

スコア52

test CHANGED
File without changes
test CHANGED
@@ -377,3 +377,173 @@
377
377
  どのように書くのが正しいやり方なのでしょうか。
378
378
 
379
379
  どなたか助言を頂けると助かります。
380
+
381
+
382
+
383
+ ### 追記
384
+
385
+
386
+
387
+ こちらのページを参考に、記述を追加しましたがエラーが出ています。
388
+
389
+ [http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner](http://bigbuddha.hatenablog.jp/entry/recyclerview-beginner)
390
+
391
+
392
+
393
+
394
+
395
+ ```Kotlin
396
+
397
+
398
+
399
+ private inner class RecyclerViewAdapter(
400
+
401
+ private val _listData: MutableList<JSONObject>)
402
+
403
+ : RecyclerView.Adapter<RecyclerViewHolder>()
404
+
405
+ {
406
+
407
+ override fun getItemCount(): Int
408
+
409
+ {
410
+
411
+ LAST_POSITION = _listData.size
412
+
413
+ return _listData.size
414
+
415
+ }
416
+
417
+ fun addItem(item: JSONObject) {
418
+
419
+ _listData.add(item)
420
+
421
+ notifyDataSetChanged()
422
+
423
+ }
424
+
425
+ override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int)
426
+
427
+ {
428
+
429
+ val list = _listData[position]
430
+
431
+ holder.tvname.text = list["name"].toString()
432
+
433
+ holder.tvkind.text = list["kind"].toString()
434
+
435
+ }
436
+
437
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder
438
+
439
+ {
440
+
441
+ val holder = layoutInflater.inflate(R.layout.row, parent, false)
442
+
443
+ return RecyclerViewHolder(holder)
444
+
445
+ }
446
+
447
+
448
+
449
+ }
450
+
451
+
452
+
453
+ private inner class scrollListener: RecyclerView.OnScrollListener() {
454
+
455
+ override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
456
+
457
+ super.onScrollStateChanged(recyclerView, newState)
458
+
459
+
460
+
461
+ if (!recyclerView.canScrollVertically(1)) {
462
+
463
+
464
+
465
+ val addlist = _list?.take(10) as MutableList<JSONObject>
466
+
467
+ val firstList = _list as MutableList<JSONObject>
468
+
469
+ _list = _list!!.drop(10)
470
+
471
+
472
+
473
+ Toast.makeText(activity, "最終行です", Toast.LENGTH_LONG).show()
474
+
475
+
476
+
477
+ for (index in 1..addlist.size) {
478
+
479
+ println(addlist[index - 1])
480
+
481
+ RecyclerViewAdapter(firstList).addItem(addlist[index - 1])
482
+
483
+ }
484
+
485
+ .
486
+
487
+ .
488
+
489
+ .
490
+
491
+
492
+
493
+
494
+
495
+ ```
496
+
497
+
498
+
499
+ リサイクラービューを設定するメンバクラスの中に、
500
+
501
+
502
+
503
+ ```kotlin
504
+
505
+
506
+
507
+ fun addItem(item: JSONObject) {
508
+
509
+ _listData.add(item)
510
+
511
+ notifyDataSetChanged()
512
+
513
+ }
514
+
515
+ ```
516
+
517
+
518
+
519
+ を追加しました。
520
+
521
+
522
+
523
+ そして上記の内容で実行すると、エラーが出てきました。
524
+
525
+
526
+
527
+ ```logcat
528
+
529
+
530
+
531
+ java.lang.ClassCastException: kotlin.collections.EmptyList cannot be cast to kotlin.collections.MutableList
532
+
533
+ at jp.wings.nikkeibp.sampleapp.RecyclerViewFragment$scrollListener.onScrollStateChanged(RecyclerViewFragment.kt:100)
534
+
535
+ at androidx.recyclerview.widget.RecyclerView.dispatchOnScrollStateChanged(RecyclerView.java:4998)
536
+
537
+ at androidx.recyclerview.widget.RecyclerView.setScrollState(RecyclerView.java:1545)
538
+
539
+ at androidx.recyclerview.widget.RecyclerView.onTouchEvent(RecyclerView.java:3212)
540
+
541
+ ................
542
+
543
+
544
+
545
+ ```
546
+
547
+
548
+
549
+ もう少し調べてみようと思います。

1

内容の修正

2020/01/21 07:50

投稿

Haruto513
Haruto513

スコア52

test CHANGED
File without changes
test CHANGED
@@ -374,4 +374,6 @@
374
374
 
375
375
 
376
376
 
377
- どのように書くのが正しいやり方なのでしょうか。どなたか助言を頂けると助かります。
377
+ どのように書くのが正しいやり方なのでしょうか。
378
+
379
+ どなたか助言を頂けると助かります。