0Pricing
Android Academy · Lesson

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 a String token.
  • 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

  1. Why Paging
  2. PagingSource and Pager
  3. Paging in Compose Lists
  4. RemoteMediator and Caching
← Back to Android Academy