Using Jetpack Paging v3

Joao Sousa
Movile Tech
Published in
3 min readAug 31, 2020

What is the Paging Library?

The Paging Library, is a library created by Google to solve the problem of data paging on Android.

A problem that we usually have to solve when creating an application is the request for long data, which does not require a single time, since the user sees only a small part of that data at a time.

One way to solve this problem is to add a ScrollListener to RecyclerView and load more data when the user reaches the end of the list. With this approach, it is difficult to maintain efficiency, also the UI logic and generates a complicated data logic, making it difficult to debug and design tests.

Benefits

  • The Paging Library helps to load and display small blocks of data at a time.
  • Loading partial data on demand reduces the use of network bandwidth and system resources.
  • Easily integrated with RecyclerView.
  • Keeps track of the keys to be used to retrieve the next and previous page.
  • Calls automatically correct page when the user scrolls to the bottom of the list.
  • Ensures that multiple requests are not triggered at the same time.
  • Tracks the loading status and allows you to display it on a RecyclerView item in the list, or anywhere else on the user interface and easily show failed loads.

News Paging 3

  • Built-in support for error handling, including reload and retry mechanism.
  • Support for Coroutines and Flow of Kotlin, as was LiveData and RxJava v2.
  • Header, footer and separator included in the library.

How to implement

First, you need to have Android Studio version 3.6 or higher.

Add the dependency on build.gradle

def paging_version = "3.0.0-alpha03" 
implementation "androidx.paging:paging-runtime:$paging_version"

At the time of writing this article, this is the current version of the library

Data Flow

Paging v3 data flow

The main component in the Repository layer is PagingSource. Each PagingSource object, defines a data source and retrieves the data from that source, be it a local database or an external API. In our example I will use an external API:

After the creation of our PagingSource in the Repository layer, we will create our Pager.

Pager is responsible for configuring the size of our requisition pages, through PagingConfig, and assisting in the data flow.

From Paging v3, it is not necessary to convert Flow to LiveData. To maintain a cache of the data loaded in our ViewModel, we call cachedIn passing our androidx.lifecycle.viewModelScope.

Now we will have our last stage of implementation, which is to pass the loaded data to our View.

You will need to collect data from PagingData and pass it to our Adapter:

  • Request data from viewModel :

The implementation of the adapter just needs to make an extension of the PagingDataAdapter class, and pass a DiffUtilCallback to differentiate the items received:

This was the necessary implementation to do pagination on Android using the Paging Library v3, thanks for reading ❤️.

The project used was shared on my Github .

Reference links with library documentation

https://developer.android.com/topic/libraries/architecture/paging/v3-overview

https://codelabs.developers.google.com/codelabs/android-paging/#0

Special thanks to MovilePay’s android team.

--

--