Note
This is a four-part series covering pagination. Here are the links:
- Paging3: Introduction <—- You are here
- Paging3: Character module
- Paging3: App module
- Paging3: Testing
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:
-
Create a
PagingSource
that defines how to load data from your data source. -
Create a
Pager
object by passing in thePagingConfig
and thePagingSource
. -
Obtain the
LazyPagingItems
by collecting on the view viaFlow<PagingData<T>>.collectAsLazyPagingItems
. -
Use the
LazyPagingItems
in your Compose UI to display the paginated data viaLazyColumn
,LazyRow
or other variants.
What will we cover?
In this series, we’ll cover the following:
- Setting up your
PagingSource
. - Setting up your
ViewModel
- Setting up your UI (Activity + Compose UI)
- Injecting all the relevant components (Optional)
- Setting up unit tests for your
PagingSource
andViewModel
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