What’s pagination
Loading an inventory of things in batches as an alternative of loading all objects in a single go is known as pagination. Once you open the Gmail net app, you see 50 emails by default in a single web page. To see the 51st mail, you need to click on on subsequent web page button.
Why pagination
Take into account in your Gmail account, there are about one thousand plus emails. Loading all a thousand emails without delay may have the next drawbacks:
- Database learn operation will probably be very pricey. So you’ll get an inventory with a large amount of latency.
- After fetching the checklist, rendering a lot of objects in a single go can gradual down GUI.
Methods to make use of pagination
In Android, there are two methods to implement pagination:
- Utilizing jetpack’s
paging
library. You possibly can see the detailed implementation of the paging library utilizing MVVM in my different weblog Paging with MVVM and coroutines. - Utilizing our personal implementation of RecyclerView scroll listener.
Easy scroll listener
This text focuses on our personal implementation. There are various methods to implement logic contained in the scroll listener of RecyclerView. I’ve created a generic implementation of scroll listener which you’ll be able to merely copy-paste into your code:
/**
* Created by Mohit Rajput on 20/03/22.
* Used for the load extra performance.
*/
summary class PaginationScrollListener(personal val layoutManager: LinearLayoutManager) :
RecyclerView.OnScrollListener() {
override enjoyable onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
tremendous.onScrolled(recyclerView, dx, dy)
val visibleItemCount: Int = layoutManager.childCount
val totalItemCount: Int = layoutManager.itemCount
val firstVisibleItemPosition: Int = layoutManager.findFirstVisibleItemPosition()
if (!isLoading() && !isLastPage()) {
if (visibleItemCount + firstVisibleItemPosition >= totalItemCount
&& firstVisibleItemPosition >= 0
) {
loadMoreItems()
}
}
}
protected summary enjoyable loadMoreItems()
summary enjoyable isLastPage(): Boolean
summary enjoyable isLoading(): Boolean
}
Right here is an instance utilization of this scroll listener:
personal var isLoadingEmails = false
personal val viewModel by viewModels<EmailsViewModel>()
personal enjoyable addScrollListener() {
recyclerView.addOnScrollListener(object :
PaginationScrollListener(recyclerView.layoutManager as LinearLayoutManager) {
override enjoyable loadMoreItems() {
viewModel.fetchEmails()
}
override enjoyable isLastPage() = viewModel.isAllEmailLoaded
override enjoyable isLoading() = isLoadingEmails
})
}
Rationalization
- Once you begin loading the checklist, set
isLoadingEmails
worth totrue
. As soon as loaded, set it tofalse
. This may be managed by view state triggered by stay knowledge or state stream. -
viewModel.isAllEmailLoaded
worth relies on API name implementation. In case your API offers the whole rely of checklist objects, you’ll be able to merely implement it asisAllEmailLoaded = currentList.measurement == totalListItems
. - This implementation is for
LinearLayoutManager
solely. I’ll present an environment friendly code forGridLayoutManager
quickly.
Conclusion
Load extra or pagination is a widespread idea that just about each app makes use of. We write the code of the scroll listener time and again. So I created a small generic class that buildings the load extra in order that it may be reused simply. If loading is in progress, it won’t make a loading name concurrently.
Thanks for the studying. 😊😊😊