7 steps to install Paging library in Android

Tram Ho

I recently learned about the Paging library – part of Android Jetpack. Although there are some resources on how to implement this library in an application, I have faced a lot of problems and have to learn more about it. So I think I will write about 7 basic steps to deploy Paging library in Android apps.

The Paging library makes it easier to load paging data in your application. The Paging library also supports large-bound, non-bound data lists (eg, continuously updating feeds). The paging library is based on the idea of ​​sending the list of data to the UI in the livedata form of the list and is observed by RecyclerView.Adapter.

I have chosen to build a news delivery application to test the Paging library. Let’s go back to the main topic, 7 steps to install the library.

I. Add Paging library into the application

II. Install Retrofit to get data from api

Create an interface ** RestApi.java **:

Then create class RestApiFactory.java :

III. Install DataSource

DataSource – This is the base class to load data, used in the list paging. DataSource can be implemented using any of the following 3 classes:

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

ItemKeyedDataSource : The first step is to define the key, the purpose is to load data on the next page based on that key. For example, data class User, the id field will usually have the key is userId. The size of the list is unknown, and retrieving the next data usually depends on the most recently known id. So if we get data for items 1 through 10 in the first result, the ItemKeyedDataSource will automatically fetch the next data from 11-20.

PositionalDataSource : This class will be useful for list supplies of fixed size, retrievable in any arbitrary position and size.

In this article, I will use PageKeyedDataSource. Take a look at the code below:

IV. Install DataSourceFactory

DataSourceFactory is responsible for accessing data using the DataSource ‘s config and the PagedList that I will create later in this article in the ViewModel class.

V. ViewModel class implementation

ViewModel will be responsible for creating PagedList along with its configs and sending it to Activity so that Activity can observe data as data changes and pass it to adapter.

Surely everyone is wondering what is PagedList ? The PagedList is a list that contains your data items and calls DataSource to load the elements. It usually consists of a work done in the background (loading data) and a job done in the foreground (updating the UI based on the downloaded data).

For example, let’s say there is some data that we add to the DataSource in the background thread. The DataSource invalidates the PagedList and updates the value. Then on the main thread, the PagedList will notify the observe elements it has new values. Now the PagedListAdapter knows about the new value.

PageList has 4 parameters that we need to pay attention to:

a. setEnablePlaceHolder (boolean enablePlaceholders) b. setInitialLoadSizeHint (int initialLoadSizeHint) c. setPageSize (int pageSize) d. setPageSize (int pageSize)

Below is the ViewModel class of the project:

BECAUSE. Install PagedListAdapter

PagedListAdapter is an implementation of RecyclerView. Adapter takes data from PagedList, it uses DiffUtil as parameter to calculate the difference of data and do all the updates for you. DiffUtil is defined in the Model Class in this case:

Class PagedListAdapter :

VII. Activity settings

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

With this article I hope everyone has a good experience and it will help everyone. Happy coding! Thanks for reading.

References:

https://proandroiddev.com/8-steps-to-implement-paging-library-in-android-d02500f7fffe https://github.com/anitaa1990/PagingLibrary-Sample

Share the news now

Source : Viblo