-
Notifications
You must be signed in to change notification settings - Fork 51
/
EndlessScrollActivity.kt
55 lines (45 loc) · 1.67 KB
/
EndlessScrollActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package smartrecycleradapter.feature
/*
* Created by Manne Öhlund on 2019-08-10.
* Copyright (c) All rights reserved.
*/
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_simple_item.recyclerView
import smartadapter.SmartEndlessScrollRecyclerAdapter
import smartrecycleradapter.R
import smartrecycleradapter.feature.simpleitem.SimpleItemViewHolder
import smartrecycleradapter.utils.runDelayed
import smartrecycleradapter.utils.showToast
class EndlessScrollActivity : BaseSampleActivity() {
var itemCount = 50
lateinit var smartAdapter: SmartEndlessScrollRecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.title = "Endless Scroll Sample"
val items: MutableList<Any> = (0..itemCount).toMutableList()
smartAdapter = SmartEndlessScrollRecyclerAdapter
.items(items)
.setAutoLoadMoreEnabled(true)
.setLoadMoreLayoutResource(R.layout.custom_load_more_view)
.setOnLoadMoreListener { adapter, loadMoreViewHolder ->
when {
itemCount < 100 -> addMoreStuff()
else -> disableScroll()
}
}
.map(Integer::class, SimpleItemViewHolder::class)
.into(recyclerView)
}
private fun addMoreStuff() {
runDelayed {
smartAdapter.addItems((itemCount + 1..itemCount + 20).toList())
itemCount += 20
}
}
private fun disableScroll() {
runDelayed {
smartAdapter.isEndlessScrollEnabled = false
showToast("No more items to load")
}
}
}