Pagination in RecyclerView without Paging Library
I recently had a requirement where I had a use pagination in a ViewPager
RecyclerView
i.e. I needed to get list of transactions using a web service and then split those transactions by status (Success, Cancelled, Failed) and display them separately in tabs using TabLayout
. Initially, I thought about using Paging Library from the Android Jetpack series.
Reason why I didn’t choose Paging Library for this scenario
But for this particular use case, Paging library would not work because there is no way to manipulate the data from the Paging lib once it is fetched from backend. At Least there is no straightforward way to do this.
A quick workaround would be to fetch the list of transactions from the web service, store them locally in Room and then query them based on status and update the UI. But this would not work because:
- App was not allowed to store the transactions locally.
- Even if I stored it locally, I needed to purge the data once the app was closed.
- There is a filter option which needed to again purge the existing data and fetch it fresh from the backend. So if I was to use Paging library, I had to invalidate the DataSource, fetch new data based on the filter options, then store it locally, then query it again, then display it.
Needless to say, this seemed to me, a little overkill.
Paginator Solution
So my solution was to use the good old way of RecyclerView.OnScrollListener
to paginate my data.
RecyclerViewPaginator.java
RecyclerViewPaginator.kt
Usage in Java:
Usage in Kotlin:
Thanks for reading guys! If you are interested in more anecdotes like this, please checkout my Github project.
Happy Coding!