This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

RecyclerView pagination scroll listener in Android




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:

  1. 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.
  2. 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
}
Enter fullscreen mode

Exit fullscreen mode

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
        })
    }
Enter fullscreen mode

Exit fullscreen mode



Rationalization

  • Once you begin loading the checklist, set isLoadingEmails worth to true. As soon as loaded, set it to false. 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 as isAllEmailLoaded = currentList.measurement == totalListItems.
  • This implementation is for LinearLayoutManager solely. I’ll present an environment friendly code for GridLayoutManager 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. 😊😊😊

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?