Related Articles

Related Articles

Getting Started with Paging Library v3 in Android
  • Difficulty Level : Hard
  • Last Updated : 12 Jan, 2021

The RecyclerView used in the Android projects displays a large list of data to the user. However, it is not considered an efficient approach. Fetching a bunch of information from the network and loading it on an application is a vital task. Moreover, while using a mobile application, the user observes only a small portion of data at a time. To address this issue, Google updated the Paging architectural components library for Android along with the release of Android 11 beta. Google released the Paging 3 library to simplify the implementation of Paging in Android applications.

Note: The Paging 3.0 library is currently in its early stages and Google released only its alpha version. The more stable version of this library is Paging 2. 

The Paging 3 library was introduced as a part of the Android Jetpack series and it is written entirely using Kotlin Coroutines. This library provides a completely new methodology to load a large set of data on an application. The approach is to fetch small chunks of data at a time, and when the user reaches the end of the list it immediately loads more data in the RecyclerView. This process is termed Paging. This Paging 3 library provides a way to load more data automatically when needed. The developers can use the Paging Library v3 to implement pagination in their app. But what’s pagination, why we need pagination? One can refer to the following diagram to get the answer.

Pagination in Android

Advantages of using Paging Library

The Paging 3.0 version is very unique in comparison to the previous versions of the Android Paging library. Following are the new features and their advantages:



  • Provides support for error handling along with refresh and retry functionality.
  • Finest Support for Kotlin coroutines, Flow, LiveData, and RxJava.
  • Built-in functionality to add loading state headers, footers, and list separators.
  • Provides in-memory caching of the paged data that assures the systematic use of device resources.
  • Keeps track of the keys in order to get data from the next as well as previous pages.
  • Prevents duplication of the API request and ensure that the app uses network bandwidth and system resources in an efficient manner.
  • Improves the repository layer by providing cancellation support and a simplified data source interface.

Using Paging library in the Android Project:

Apply the Paging 3.0 library in a project by adding its implementation in the app-level build.gradle file to import the Paging components.

dependencies {

 def paging_version = “3.0.0-alpha11”

 implementation “androidx.paging:paging-runtime:$paging_version”

}

In order to use RxJava or LiveData, one needs to add the below implementation:

// RxJava2 support – optional

implementation “androidx.paging:paging-rxjava2:$paging_version”

// Guava ListenableFuture support – optional

implementation “androidx.paging:paging-guava:$paging_version”

The Architecture of Paging Library

Paging library requires the proper separation of the project’s concerns and thus it integrates the recommended Android architecture pattern. The components of the library work in three layers of an application:

  1. The Repository layer
  2. The ViewModel layer
  3. The UI layer

The Architecture of Paging Library

  • Repository layer: This layer contains components namely PagingSource and RemoteMediator. The task of PagingSource is to define a source of data and to fetch the data from that source. It is capable of loading data either from a local database or any network source. The RemoteMediator component controls the paging over any layered data source.
  • ViewModel: The instances of PagingData is created by a Public API provided by the Pager component of ViewModel. This PagingData is exposed in reactive streams that connect the ViewModel to the UI layer.
  • UI: The visual representation of the paginated data is represented by this layer. Its primary component is PagingDataAdapter that is a RecyclerView to handle the paginated data.

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :