Pagination with RecyclerView

Etienne Lawlor
Oct 15, 2015 · 3 min read

Almost every API we encounter as developers requires you to handle pagination. When querying an API for some resource instead of delivering all of the results, which could be time consuming and cumbersome to deal with, an API will typically make you to paginate through the results. Of course if you are trying to support pagination on a client you need to handle it gracefully.


The RecyclerView is a powerful, flexible API which is deemed as the replacement to ListView, GridView, and some other ViewGroups.

It requires you to use the ViewHolder pattern which improves performance as you avoid initializing views every time.

You can decide how you want the items in your adapter to be arranged through the use of LayoutManagers.

LinearLayoutManager — supports both vertical and horizontal lists

StaggeredLayoutManager — supports Pinterest like staggered lists

GridLayoutManager — supports displaying grids as seen in Gallery apps

ItemAnimators are used to provide animations when items are added or removed from an adapter.

If you want to add margins, borders, or dividers between items in the adapter you need to set up an ItemDecorator.

The easiest way to see how all of this gets wired up is with a demonstration. For my example, I am working with the Vimeo API and making a call to the Videos endpoint.

Set up your RecyclerView first. I have chosen a LinearLayoutManger here and set up an ItemAnimator so that items will slide up when added and slide down when removed. Call setAdapter() to wire up the adapter to the RecyclerView. Then don’t forget to add an OnScrollListener.

layoutManager = new LinearLayoutManager(getActivity());
videosAdapter = new VideosAdapter();
recyclerView.setItemAnimator(new SlideInUpAnimator());

// Pagination

Let’s see what that OnScrollListener looks like. The responsibility of this listener is to load more items only if some conditions are satisfied. If you aren’t currently loading items and the last page hasn’t been reached then it checks against the current position that is in view to decide whether or not to load more items.

Ultimately you will be making two different API calls, an API call for the first page and an API call for the next page. Here is what the call to the first page looks like.

Call findVideosCall = vimeoService.findVideos(query,

And here is the call to the next page.

Call findVideosCall = vimeoService.findVideos(query,

The call to get the first page has a callback method to handle the response. First you add all the videos from the response to the adapter. Then you check if the number of videos is greater than or equal to the PAGE_SIZE. If that is the case then add a loading item to the adapter otherwise set the flag isLastPage to true to prevent loading more items.

Similarly the call to get the next page has a callback method to handle the response. This time, when the response returns you want to remove the loading item before adding items to the adapter.

The complete example of how I set all of this up is in my repo


The files you should focus on are :

Java files

Xml files

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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