Note

This is a four-part series covering pagination. Here are the links:

What is Paging3?

Android Paging3 is a Library provided by the Android Jetpack components that helps you load and display data from large datasets efficiently. It’s designed to handle the challenges of loading data in chunks or pages, such as handling network latency, managing data in memory, and providing a smooth user experience.

The Paging3 library introduces a new architecture that is built around three main components: PagingSource, PagingData, and PagingDataAdapter.

How is it used?

The equivalent component to PagingDataAdapter from the Paging3 library is LazyPagingItems. LazyPagingItems is part of the paging-compose artifact and is specifically designed to work with Jetpack Compose.

To use LazyPagingItems, you need to follow these steps:

  1. Create a PagingSource that defines how to load data from your data source.

  2. Create a Pager object by passing in the PagingConfig and the PagingSource.

  3. Obtain the LazyPagingItems by collecting on the view via Flow<PagingData<T>>.collectAsLazyPagingItems.

  4. Use the LazyPagingItems in your Compose UI to display the paginated data via LazyColumn, LazyRow or other variants.

What will we cover?

In this series, we’ll cover the following:

  1. Setting up your PagingSource.
  2. Setting up your ViewModel
  3. Setting up your UI (Activity + Compose UI)
  4. Injecting all the relevant components (Optional)
  5. Setting up unit tests for your PagingSource and ViewModel

Setup

In this sample, we’ll divide things into 4 modules:

  • app
  • character - contains all character related data
  • di (JVM module)
  • di (Android module)

NOTE: You can merge the di modules but let’s ignore that for now

We’ll start off with the character module, followed by the app module and end with the di modules

For this series, we’ll be using Rick and Morty REST APIs

Project

Your root build.gradle can be setup however you like but if you don’t know where to start, take a look at the build.gradle file here. Also take a look at the version catalog here which contains all the dependencies used in this project

You can take a look at the full project here