Paginator — simple pagination in Android

Natesh Relhan
Curofy Engineering
Published in
3 min readJun 13, 2017

In this post I’ll show you how to easily add scroll listener to recycler view with the help of dagger and java reflection apis.

Earlier, for pagination in an activity/fragment containing list of items, I had to create an object of scroll listener then attach it to a recycler view and also specify method i.e to be called when user scrolls down the page:

So, let’s say we have 5 different activities/fragments, we need to type the above mentioned code in each activity/fragment which will result in redundancy and more lines of code => to create an object of scroll listener, specify method (i.e pagination method) and then attach it to a recycler view.

This redundancy is solved by reducing the lines of code using dagger (to inject object in each activity/fragment) and java reflection api (to identify method called, after user scrolls down the page) because developers just love less code.

Let’s start with the explanation of this module.

  • Annotate layout manager and method which is to be called after user scrolls down the page:
@PaginateManager on LayoutManager
@Paginate on pagination method
  • Just keep in mind to instantiate layout manager object before injecting Paginator component or calling it’s constructor as Paginator is dependent on it:
  • I am using dagger in my app but you can also manually create paginator object.

That’s the whole concept of Paginator, you just need to inject/create it’s object and you are ready to implement pagination in any activity/fragment with fewer lines of code #lessCode. Source code and sample class are mentioned at the end.

Note: EndlessRecyclerScrollListener which is used in this module is just an example, you can use the same or set it up according to your requirements.

Optional: If you are using Proguard in your app, you need to add the following lines in proguard settings file:

#Paginator used as endless scroll listener
-keep @interface com.curofy.internal.Paginate
-keep @interface com.curofy.internal.PaginateManager
-keepclassmembers,includedescriptorclasses class * {
@com.curofy.internal.Paginate *;
@com.curofy.internal.PaginateManager *;
}

Resources:

--

--

Natesh Relhan
Curofy Engineering

I build things. <Google Certified Associate Android Developer>