PagingSource and Pager
Define how pages are loaded.
Defining How Pages Load
To page from a network API you write a PagingSource. It answers two questions for Paging 3:
- How do I load the page for a given key?
- If the user refreshes, where should I resume from?
You then wrap it in a Pager that turns it into a Flow<PagingData>.
PagingSource Type Parameters
PagingSource<Key, Value> takes two type parameters:
- Key - identifies a page. For a page-number API it is
Int; for a cursor API it is aStringtoken. - Value - the item type, e.g.
Article.
import androidx.paging.PagingSource
import androidx.paging.PagingState
class ArticlePagingSource(
private val api: ArticleApi
) : PagingSource<Int, Article>() {
// implement load() and getRefreshKey()
}All lessons in this course
- Why Paging
- PagingSource and Pager
- Paging in Compose Lists
- RemoteMediator and Caching