Paging3: Introduction
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:
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