Using the Paged List with Boundary Callback in Android

Vivek Maskara
Jun 15, 2020 · 3 min read
Image for post
Image for post

In this post, I will explain how to use the Android Paged list with a boundary callback. The idea is to see an example where both the network and the local database is involved. We would be using Kotlin for the whole example.

Introduction

  • The RecyclerView will observe the local DB to display the items in the list.
  • The calls are paginated ie. at a time 10–20 items are loaded and more items are loaded when the user scrolls down.
  • Whenever a boundary condition is met(start/end of the list), a network call is made to fetch new data. The data is fetched and inserted in the DB.
  • As the RecyclerView is already observing the DB, it populates the list with new data when required.

Prerequisite

Dependencies

implementation "androidx.paging:paging-runtime-ktx:2.1.2"    implementation "androidx.paging:paging-rxjava2-ktx:2.1.2"

Add a Recycler View holder

Add a Recycler View Adapter

In the adapter, we add a DIFF_CALLBACK object. This is used by the PagedListAdapter to compare two objects and check if they are the same.

Define a Data Source

Define a Boundary Callback

  • onZeroItemsLoaded : It is triggered when the list has no items.
  • onItemAtFrontLoaded: It is triggered when the user scrolls to the top of the list.
  • onItemAtEndLoaded: It is triggered when the user scrolls to the end of the list.

For each of these functions, we do an API call to fetch data from the network. Once fetched, the data is inserted into the DB. You can have some other custom logic based on your use-case.

Note: In the example, I am not doing the actual API call but am just using mockGetUsers to get a list of 10 random users.

Next, we will wire up everything.

Setup Live Data

In the above snippet, we are creating an instance of LivePagedListBuilder . Note the following about this object:

  • It used a pagedListConfig that defines the size of a page and pre-fetch distance. Pre-fetch distance tells the list the number of items to be fetched initially.
  • It is wired to the fetchUsers method of userDao. Remember that fetchUsers returns a DataSource.Factory.
  • Finally, we provide an instance of UserBoundaryCallback to trigger the network calls.

Setup the Recycler View

Demo

Image for post
Image for post
Android Paged List

Source Code

Quick Code

Find the best tutorials and courses for the web, mobile…

Sign up for Developer Updates

By Quick Code

Receive weekly updates about new posts on programming, development, data science, web development and more Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Vivek Maskara

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Vivek Maskara

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store