Pagination
2.0.0 · Feature · Presentation Module
From 2.0.0
using pagination with RecyclerView
will be implemented using PagingDataAdapter
.
The ViewModel
¶
Collect your data in Viewmodel
from data layer like this:
SomeViewModel.kt
@OptIn(ExperimentalCoroutinesApi::class)
fun getUsersFromPage() {
resultApi.loading()
useCase.getUserFromPage()
.cachedIn(viewModelScope)
.compose(flowableScheduler())
.subscribe({
resultApi.success(it)
}, {
genericErrorHandler(it, resultApi)
}).let(disposable::add)
}
Two things to note is to call the cachedIn
to store the response as cache and to use flowableScheduler()
insider the compose()
method.
The Adapter
¶
In Activity/Fragment, setup your RecyclerView
by calling setupPagination()
method:
val adapter = binding.rvUserAll.setupPagination {
withLayoutManager(LinearLayoutManager(binding.rvUserAll.context))
withBinding<ItemUserBinding> { data, userBinding ->
// (1)
}
withClick { user ->
// (2)
}
withLongClick { user ->
// (3)
}
}
// Submit the data from observer like this:
vm.resultApi.observe(this) {
when (it) {
is VmData.Success -> adapter.submitData(lifecycle, it.data)
}
}
- Define the layout and the binding of the model object to the layout
- Action when item is clicked
- Action when the item is long clicked