When using the recycler view, we would always face some kind of issues with respect to performance and smoothness. One such issue occurs because of loading many items together which causes network and performance bottleneck. To prevent this, Android introduced a Paging Library under Jetpack Libraries. This library helps us in implementing pagination to our Recycler View irrespective of the data source.

If you have previously used your own pagination logic using Recycler View listeners, this library would help ease it for you and with MVVM Architecture, it will be a lot cleaner than before.

I assume you have prior knowledge of implementing Recycler View in MVVM Architecture.

Here in this example, we are trying to load upcoming cricket matches from our backend using API calls.

Let’s talk Implementation!


First Step —

Add the required dependencies for the Pagination Library-


Second Step —

Add a Recycler View inside the XML and use a custom BindingAdapter to pass in an adapter. We define the adapter as below —


All are basic stuff except we use DiffUtil.ItemCallback to check if the items are the same and pass them in the constructor. We also extend our adapter class with — ‘PagedListAdapter’ instead of ‘RecyclerView.Adapter’ directly.

Once done with the adapter, we will now create two important components —

  1. DataSource
  2. DataFactory

Third Step —

We will create a class that extends DataSource. The DataSource class is where the business logic is written to fetch the data and return it. We can create a DataSource class by extending either PageKeyedDataSource (Used to load previous/next pages), ItemKeyedDataSource (Uses N-1 data items to load N item), or PositionalDataSource (Load pages of requested size at arbitrary positions).

In this example, we will use PageKeyedDataSource.

There are three methods which we will override — loadInitial, loadBefore, loadAfter. As the name suggests, these methods are used to load the initial list, the previous page, and the next page respectively. We will use a static PAGE_SIZE Int and return the key in the callback of each method we override.

Fourth Step —

We will create our DataFactory extending — ‘DataSource.Factory’.

Here, we have used the create method to initialize our DataSource class. The factory will be used in the LivePagedListBuilder which we will use to update our RecyclerView from View.

Fifth Step —

Once done with creating DataSource and DataFactory, we will now used the PagedListConfig to initialize paging which uses the DataFactory inside our ViewModel class.

We will define if we need null placeholders to show up and what the page size would be. Then we will create a LiveData of the ‘PagedList’ type and initialize it with the ‘LivePagedListBuilder’.

Sixth Step —

In the View, we will observe the LiveData and submit data to the created adapter —

Voila! We are done with using the new Paging Library from the Android Jetpack Library in our application.

Here’s a small animation of how the data flows—

Android Paging Library Animation
Android Paging Library

MVVM Series –

Leave a Reply

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