Load More In RecyclerAdapter - Krunal-Kevadiya/kotlin-common GitHub Wiki
class LoadMoreActivity : AppCompatActivity() {
private lateinit var noPaginate: NoPaginate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_loadmore)
setupLoadMore()
}
private fun setupLoadMore() {
noPaginate = NoPaginate {
loadingTriggerThreshold = 0
recyclerView = recyclerViews
loadingItem = LoadingItem.DEFAULT
errorItem = ErrorItem.DEFAULT
direction = Direction.UP
onLoadMore = {
noPaginate.showError(false)
noPaginate.showLoading(true)
Handler(Looper.getMainLooper()).postDelayed({
if (Random(25).nextInt() % 2 == 0) {
count++
noPaginate.showLoading(false)
noPaginate.setNoMoreItems(count > 3)
recyclerView.post {
val list = MutableList(10) { index -> "LoadMore -> ${index + (adapter.itemCount + 1)}" }
adapter + list
}
} else {
noPaginate.showLoading(false)
noPaginate.showError(true)
}
}, 5000)
}
}
}
override fun onDestroy() {
noPaginate.unbind()
super.onDestroy()
}
} noPaginate.showLoading(false)
noPaginate.showError(false)
noPaginate.setNoMoreItems(false); //Method onLoadMore will not to call
noPaginate.unbind(); //Don't forget call it on onDestroy();For custom error and loaging item just implement the interfaces ErrorItem or LoadingItem
class CustomErrorItem : ErrorItem {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_error, parent, false)
return object : RecyclerView.ViewHolder(view) {}
}
override fun onBindViewHolder(
holder: RecyclerView.ViewHolder,
position: Int,
repeatListener: OnRepeatListener?
) {
val btnRepeat = holder.itemView.findViewById<Button>(R.id.btnRepeat)
btnRepeat.setOnClickListener {
repeatListener?.onClickRepeat()
}
}
} class CustomLoadingItem : LoadingItem {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_loading, parent, false)
return object : RecyclerView.ViewHolder(view) {}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {}
}noPaginate = NoPaginate {
loadingTriggerThreshold = 0
recyclerView = recyclerViews
loadingItem = CustomLoadingItem()
errorItem = CustomErrorItem()
direction = Direction.DOWN
onLoadMore = {}
}- Double-sided pagination
- Delegate for Presenter or Interactor, with implementation Limit/Offset and Page pagination