7 steps to implement Paging library in Android

Anitaa Murthy
ProAndroidDev
Published in
4 min readJul 2, 2018

--

I had a chance to explore the Paging library recently that is part of the Android Jetpack. While there were some resources on how to implement this in an app, I faced a lot of issues and had to spend the better part of a Sunday working on it. So I thought I would write about the 7 basic steps to implement the Paging library in an Android app.

Paging library makes it easier to load data gradually in your app. The Paging library supports both large bounded list and unbounded lists, such as continuously updating feeds. The Paging library is based on the idea of sending lists to the UI with the live data of a list that is observed by RecyclerView.Adapter.

So I have chosen to build a news feed app to test the paging library. This is the app.

The final app

For those of you interested in skipping the article, the GitHub link is here.

So let’s begin!

Step 1: Add the Paging library to the app

Step 2: Setup retrofit to fetch news feed

So this should be pretty self explanatory. We are creating an interface class RestApi.java

And a service class: RestApiFactory.java

Step 3: Setup the dataSource

DataSource — This is the base class for data loading, used in list paging. DataSource can be implemented using any one of these 3 classes:

PageKeyedDataSource: We can use this class if we need to load data based on the number of pages in the dataSource. For instance, we pass page number as a query parameter in the request. The page number will increment sequentially until all the pages are fetched and displayed.

ItemKeyedDataSource: The first step is to define the key that will be used to determine the next page of data. For instance, if it is a User class, then the id would typically be the userId value. The size of the list is generally unknown, and fetching the next set of the data usually depends on the last known Id. So if we fetch items 1 to 10 in the first result, then ItemKeyedDataSource will automatically fetch the next set of 11 to 20.

PositionalDataSource: This class would be useful for sources that provide a fixed size list that can be fetched with arbitrary positions and sizes.

In this scenario, we would be using a PageKeyedDataSource. The following code shows how we can create PageKeyedDataSource for our FeedDataSource class.

FeedDataSource.java

Note: I’m using LiveData to push the network updates to the UI. This is because it’s lifecycle aware and will handle the subscription for us.

Step 4: Setup the DataSourceFactory

DataSourceFactory is responsible for retrieving the data using the DataSource and PagedList configuration which we will create later in this article in our ViewModel class.

Step 5: Setup the ViewModel

The view model will responsible for creating the PagedList along with its configurations and send it to the activity so it can observe the data changes and pass it to the adapter.

So what is a PagedList? PagedList is a wrapper list that holds your data items (in our case the list of articles we need to display) and invokes the DataSource to load the elements. It typically consists of a background executor (which fetches the data) and the foreground executor (which updates the UI with the data).

For instance, let’s say we have some data that we add to the DataSource in the background thread. The DataSource invalidates the PagedList and updates its value. Then on the main thread, the PagedList notifies its observers of the new value. Now the PagedListAdapter knows about the new value.

A PageList takes in 4 important parameters:

a. setEnablePlaceholders(boolean enablePlaceholders) — Enabling placeholders mean there is a placeholder that is visible to the user till the data is fully loaded. So for instance, if we have 20 items that are needed to be loaded and each item contains an image, when we scroll through the screen, we can see placeholders instead of the image since it is not fully loaded.
b. setInitialLoadSizeHint(int initialLoadSizeHint) — The number of items to load initially.
c. setPageSize(int pageSize) — The number of items to load in the PagedList.
d. setPrefetchDistance(int prefetchDistance) — The number of preloads that occur. For instance, if we set this to 10, it will fetch the first 10 pages initially when the screen loads.

Below is the ViewModel class implemented for this project:

FeedViewModel.java

Step 6: Setup the PagedListAdapter

PagedListAdapter is an implementation of RecyclerView.Adapter that presents data from a PagedList. It uses DiffUtil as a parameter to calculate data differences and do all the updates for you.

The DiffUtil is defined in the Model Class in our case:

Article.java

The PagedListAdapter class for this project:

FeedListAdapter.java

Note: Notice the class extends PageListAdapter and does not override the usual getItemCount() as this is provided by the PageList object. If we do need to override this method, we need to add super.getItemCount() to the method.

Step 7: Setup the Activity

The last step is to setup our Activity class with the ViewModel, RecyclerView, PagedListAdapter:

FeedActivity.java

And that’s it guys! I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.

Happy coding! Thanks for reading!

--

--