A custom pagination in Compose — An alternative to the Android Paging Library

Barros
5 min readFeb 23, 2023
Photo by Mikołaj on Unsplash

The idea of this article is to propose an alternative to the Android Paging Library that it could handle the pagination without using external libraries, also we are looking for a method that it saves internally the result (offline mode) and that it could be consulted in a pagination way (like with a Remote Mediator).

Offline with Room

To handle the pagination offline, we have to save the results fetched from network to our local database, in this case we use Room for this purpose, you can follow the guide in the official guideline to set your database.

The key point of this implementation is to set a Query that provides exactly which page we want:

@Query("SELECT * FROM beers WHERE (:beerName IS NULL OR name LIKE :beerName) ORDER BY id ASC LIMIT :limitPerPage OFFSET :offset")
suspend fun getBeers(beerName: String?, offset: Int, limitPerPage: Int): List<BeerDatabaseModel>

In this case, we asked to the database to return a sorted list by id of BeerDatabaseModel, filtered by limitPerPage and offset parameters:

  • limitPerPage , it indicates how many items per page using the property LIMIT;
  • offset , it indicates from which index starting to return a chunk of…

--

--