Fetching a long list of data from the server is a very costly operation. It takes too long and too much memory. In those situations, the backend will split the list into pages. That is called pagination.
We are all familiar with androidx.paging libraries. The most recent version is Paging 3. I am gonna show you how to use the Paging 3 library in Jetpack Compose.
“The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.
The components of the Paging library are designed to fit into the recommended Android app architecture, integrate cleanly with other Jetpack components, and provide first-class Kotlin support.” According to the Paging official documentation
Let’s start by adding the dependency:
implementation "androidx.paging:paging-compose:1.0.0-alpha16"
Note: Check if there is a newer version of this dependency.
We will need some data to showcase pagination. For that, we will use Tasty APIs. At this link, you can find the API that we are gonna use.
Except for the paging library, I am using Hilt, Retrofit, OkHttp, Moshi, Coil, and Accompanist’s Placeholder. Make sure to add all those libraries. The link to my GitHub repo will be at the end of the article.