Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Paging 3 — Easier Way to Pagination

 

 
Metin Özcura
Senior Android Developer @Mobven
Published: January 29, 2021
Tweet
Share
 

 

Source: Android Developers Blog

 

Most apps display a large list of data, however, users generally see only a small part of it, so fetching all of the list data from the network is not an optimum solution.

The Paging library is a much easier way to implement this approach. Currently, Paging 3 is available as an alpha release. If you want to use a stable version you can use Paging 2.

This post will be about Paging 3.

 

Image for post

 

Source: Android Developers Blog

 

What’s new

Paging 3 comes with some considerable differences from the earlier versions. Some of them are:

  • Kotlin Coroutines support with Flow (also has support for LiveDatawith ListenableFuturePagingSource and RxJavawith RxPagingSource)
  • Built-in support for loading state headers and footers
  • List separators
  • Built-in support for the refreshing data, retry and error handling

 

Implementing Paging 3 in your app

Setup

Add the following dependencies to your app-level build.gradle file to import Paging 3 components.

 

dependencies {
def paging_version = "3.0.0-alpha12"

implementation "androidx.paging:paging-runtime:$paging_version"

// alternatively - without Android dependencies for tests
testImplementation "androidx.paging:paging-common:$paging_version"
}

 

Creating a Data Source

We have the option to get data and cache in a Room database to add an offline usage feature with a RemoteMediator. But this case will be in another post. In this post, PagingSource will be used to get data from The Rick and Morty API and show all the characters in the series using Paging 3.

Let’s start with creating the response model.

 

 

As you see in the PageInfo class, we have the number of pages, link to the next and previous keys. These keys will be used in the PagingSource will be created.

PagingSource

A PagingSource retrieves data from a single data source like network, local database, file, etc.

PagingSource takes two parameters, a key and a value. The Valueparameter is the type of data that will be loaded and Key defines what data to load. E.g. Int as a page number or String as a next page token.

 

Image for post

How load() function uses and updates the key. Source: Official Docs of Paging 3

 

The load() function should be implemented to retrieve data from the data source (network in our case). load() is a suspend function, so you can call other suspend functions here like a network call. For more information about suspend functions, check the Kotlin Coroutines docs.

 

 

  • If the getAllCharacters()request is successful, return the character list wrapped in a LoadResult.Page object and add the previous and next keys if they are available.
  • If the request fails, return the exception wrapped in a LoadResult.Errorobject containing info about the exception.

Pager

The container for the data returned from PagingSource is called PagingData. A Pager instance has to be created to build a PagingDatastream. Three parameters are needed to create an instance. These are:

  • PagingSource is our data source created in the name CharactersPagingDataSource.
  • PagingConfig defines how to get data from the PagingSource like page size, prefetch distance, etc. Check official docs for the parameters you can set for PagingSource.
  • An optional initialKey to start loading with a default key.

 

 

  • When the Pager object is created, it calls the load()function of the pagingSourceFactory it has.
  • cachedIn() is used to persist the data beyond configuration changes. The best place to do this in a ViewModel, using the viewModelScope.
  •  

Connect RecyclerView to the PagingData

A PagingDataAdapter should be implemented to consume PagingData.

 

 

 

The last step is collecting the PagingData object in our fragment or activity and set into the CharacterAdapter.

 

 

The RecyclerView now displays data coming from the data source and loads the next pages when the user scrolls to the end of the list.

Check the repository for full implementation.

 

metinozcura/RickAndMortyAPI

Contribute to metinozcura/RickAndMortyAPI development by creating an account on GitHub.

github.com

 

Conclusion

The Paging 3 library provides first-class Kotlin support, designed to fit recommended Android app architecture and works seamlessly with other Jetpack components. It also can handle easy or complex data operations like data loading from the network, database, or a combination of different data sources.

In the next posts, I will write about headers and footers, list separators and use the Room database and network together with RemoteMediator.

 

 

 

 

Tags: Android, Android App Development, Paging Library, Recyclerview, Kotlin

 

View original article at: 


 

Originally published: January 23, 2021

Android News
Our Engineering Roadmap
Our Engineering Roadmap

By Mark Ng

We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.

By ProAndroidDev -
Android News
Android Activity Lifecycle considered harmful
Android Activity Lifecycle considered harmful

By Eric Silverberg

The Android activity lifecycle is one of the first architectural concepts learned by a new Android developer. Ask any Android dev and they’ll likely all have seen this handy diagram from Google: 

By ProAndroidDev -
Android News
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App

By Alejandro Weichandt

It has been a year since we started working on the Android version of the mobile app at Sync. During that year, we faced more than once that moment when we had to choose which path to follow on an Architectural decision. This story is about Navigation.

By ProAndroidDev -
Android News
Custom KotlinX Serializers
Custom KotlinX Serializers

By Jobin Lawrance

Let’s say we have a third-party class that we are using as a type in one of our data class that we want to be serialized, then we have to write a custom serializable for @Serializable to work.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One