Implementing Pagination Scroll Listener in Android
Posted By : Karan Jeet Singh | 25-Apr-2019
Introduction
While browsing news or shopping apps we see literally thousands of products and services. Needless to say, that is scrolling is one of the most popular gesture in touch screen phones as we keep going down on our feeds and home pages and the app continues to show more and more data for that matter. But ever wondered from a technical point of view, how it all works?
Problem
Now when we request data using an API, the API may tend to supply all that data at once or in just one go. That increases the loading time significantly longer and tedious which frankly is not a viable solution unless you are sure that data you are going to receive is smaller. So that's why request data in bits. The art behind requesting data in bits by continuously loading of things is achieved by Pagination Scroll Listener. This blog will make you familiar with implementing Pagination Scroller in Recycler View widget on Android Platform.
Solution
First things first, we need to set up a listener in the form of an abstract class. And that listener class should extend another abstract class called OnScrollListener of the RecyclerView.
Here is an excerpt of a common Pagination Scroll listener class:
public abstract class PaginationScrollListener extends RecyclerView.OnScrollListener {
private LinearLayoutManager layoutManager;
public PaginationScrollListener(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0) {
loadMoreItems();
}
}
}
protected abstract void loadMoreItems();
public abstract int getTotalPageCount();
public abstract boolean isLastPage();
public abstract boolean isLoading();
}
There are also two important methods that should be part of the Recycler View adapter in order to consume data continuously.
public void updateListData(ArrayList<Content> listData) {
notficiationList = listData;
}
public void addData(ArrayList<Content> listData){
notficiationList.addAll(listData);
notifyDataSetChanged();
}
The setting up of Recycler View with Scroll Listener is done on the basis of it, Layout Manager. Next step is to set up the Pagination Scroll Listener on Recycler View like this:
mLayoutManager = new LinearLayoutManager(activity);
notificationsAdapter = new NotificationsAdapter(activity);
rvNotifications.setLayoutManager(mLayoutManager);
rvNotifications.addOnScrollListener(new PaginationScrollListener(mLayoutManager) {
@Override
protected void loadMoreItems() {
Logger.LogError("Current Page", "" + currentPage);
isLoading = true;
currentPage += 1;
getNotifications();
}
@Override
public int getTotalPageCount() {
return TOTAL_PAGES;
}
@Override
public boolean isLastPage() {
return isLastPage;
}
@Override
public boolean isLoading() {
return isLoading;
}
});
The three attributes of the Scroll Listener TOTAL_PAGES, isLoading and isLastPage are determining factors of new data loading, which are assigned new values on the success of the API.
isLoading = false;
TOTAL_PAGES = notificationResponse.getResponseObject().getTotalPages();
isLastPage = notificationResponse.getResponseObject().getLast();
An important point that should be noted while designing the Scroll Listener is that the adapter of the Recycler View should only be initialized once. Because every time if the adapter of the recycler view is initialized on APIs success, the new data will not be loaded at the bottom of the recycler view.
Conclusion
The use of Pagination Scroll Listeners should be exercised enough if the number of data to be loaded is ideally in hundreds otherwise your app may have performance issues in the long run when the dynamic data increases.
References
-
https://blog.iamsuleiman.com/android-pagination-tutorial-getting-started-recyclerview/
-
https://guides.codepath.com/android/endless-scrolling-with-adapterviews-and-recyclerview
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Karan Jeet Singh
Karan Jeet Singh's main area of interest is Android Development. He is familiar with Kotlin and C# in other programming languages. He is familiar with tools such as Android Studio, Visual Studio, NetBeans, Eclipse IDE.