Jetpack Paging Library in Android with Room Database.

Satyam Gondhale
3 min readSep 12, 2020

--

We all know that Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.

The Paging Library helps you load and display small chunks of data at a time. Loading partial data on demand reduces usage of network bandwidth and system resources.

Link For more details on Jetpack

Why to learn & use the Paging Library ?

  1. Loading whole data at once will consume more bandwidth uselessly also creating a large list at once use more system resources resulting in a lagging app and bad user experience
  2. To avoid above problems we use Paging Library
  3. In current Post, we will focus on displaying List (with paging library)

Important components for Paging

  1. Paged List
  2. Data Source
  3. PagedListAdapter

PagedList

This component is responsible for loading chunk’s of our app data or pages. When more data is needed it’s paged in existing PagedList object. If any loaded data changes, new instance of PagedList is emitted to observable data holder

Data Source

PagedList loads an up-to-date snapshot of our app data from corresponding DataSource object. Data flows from Backend or app Database into PagedList

PagedListAdapter

For showing normal data in RecyclerView we use RecyclerView.Adapter as a Base class but while showing PagedList data we have to use PagedListAdapter to load items in RecyclerView. This adapter is responsible for fetching and displaying the content as it is loaded.

Implementation Steps

  1. Add a Paging Library Dependency in build.gradle file of project
implementation 'androidx.paging:paging-runtime:2.1.2'

2. Create a DataSource.Factory in DAO for getting data which we are going to supply to PagedList

@Query("SELECT * FROM friend_list")
DataSource.Factory<Integer, Friend> getFriendListPaged();

3. LivePagedListBuilder is responsible for creating PagedList using DataSource.Factory & PagedList.Config objects

4. In Room Database implementation we retrieve data from Repository, so final PagedList data will be returned from Repository like this

5. But in above code we can see PagedList.Config is required for creating PagedList data. How to create it ? 😐We can create it like this

.setPageSize : Defines item loaded at once from data source. It should be number of visible item on screen. Small page size is preferred

.setInitialLoadSizeHint : Defines items to load when initial first data load occurs. The value should be larger than page size to avoid small scrolls by user to load data

.prefetchDistance : Defines how far from loaded content an access must be trigger for further loading. Should be number of visible items on screen

6. PagedListAdapter for RecyclerView is created as

7. Final part for displaying PagedList is as

This is all about Implementation of Paging Library in Android.

Repository link is shared below :

  1. Master branch has Room Database Implementation.
  2. PagingLibraryImplementation branch has Paging Library implemented along with Room Database.

Feel free to share comments. If liked Claps are welcome.😉 😊

--

--

Satyam Gondhale
Satyam Gondhale

Written by Satyam Gondhale

Associate Consultant (Globallogic)

Responses (1)