Android Paging Library Step By Step Implementation Guide
One problem with (almost all) Android app we build is we request large data which we do not require at a single time as a user only sees a small chunk of data at a time. One way to solve this problem is to listen to the scroll of RecyclerView & load more data when a user reaches the end of RecyclerView. In this approach it was difficult to maintain efficiency, also UI logic & data logic get complicated which makes debugging & testing of the project difficult.
For the rescue here comes a library from Android Paging Library. Paging Library is part of Android Architecture Components.
The Paging Library makes it easier for you to load data gradually and gracefully within your app's RecyclerView.
In this article, instead of writing the theoretical detail of Paging Library, I will show step by step guide to implement Paging Library from scratch. Also, at last, I will provide a summary of how Paging works.
So, let's start ….
For this project we will be using API form News API, We will build an App that lists the latest headlines of sports.
First thing first, Create an Android Project & don’t forget to include Kotlin Support.
Then add required dependencies
We will use Retrofit with RxJava2 to fetch news from API.
Now create a Response model, Network Service & enum State to handle the state of network call.
Now let's create one of a key component of Paging Library: DataSource
Data Source can extend one of ItemKeyedDataSource, PageKeyedDataSource or PositionalDataSource. Subclass to extend is selected according to the structure of API. For this sample app, we will use PageKeyedDataSource as we can request data for page & limit.
Now let's create NewsDataSourceFactory which will provide DataSource.
Now we will create NewsViewModel, which creates PagedList configuration & provide live list & live status. Our view model extends ViewModel from Android Architecture Components.
Further, we need layouts: list screen, news list item & news list footer
To show news & footer we need view holders
Now let's create an adapter for the news list, Adapter for paged list extends PagedListAdapter & efficiently handles item change with animation.
Now let's create news list activity which observes paged list & state from view model & submits to list adapter. It also maintains state in UI according to the state observed from the view model.
Update your Manifest file & don’t forget to add INTERNET permission.
We are done !!! Now run your app .. If you followed me currently then you will see the beautiful list with pagination.
If you missed something don’t worry, here is the full project.
AndroidPagingLibraryStepByStepImplementationGuide - Android Paging Library Step By Step Implementation Guide
In this article instead of the inner detail of Paging Library… I demonstrated step by step implementation of Paging Library.
Here is a summary in short how Paging Library works ….
- Data comes from DataSource (REST API) on a background thread.
- DataSource will update the PagedList value.
- PagedList notifies its observers of the new list on the main thread.
- PagedListAdapter will receive the new list.
- PagedListAdapter will compute the changes using DiffUtil on background thread & returns the result on the main thread.
- PagedListAdapter now calls OnBindViewHolder with updated data.
At first steps to implement Paging Library might take longer than the traditional approach to handle pagination(Add scroll listener to find if user has reached to end & then calling API for next page).
But Paging Library adds abstract layers between UI logic and data logic which makes code more clean & testable. Also, Paging Library is highly configurable with its PagingList Configuration(page size, initial page size, placeholder),
Also, Paging Library efficiently handles fetching data from server & updating on UI with minimum changes.
Thanks for reading … feedback, suggestion & queries are highly welcomed !