Endless scrolling with RecyclerView

In the previous post I promised to write about doing endless scrolling with RecyclerView and displaying a progress bar at the footer while downloading data from an external database. After playing with it for hours I finally managed to do it and I’m sharing it with you.

We will do the same effect using the same technique what we did with ListViews. The only difference is that RecyclerViews don’t have footers so we need to use two different ViewHolders. But that was my problem to figure out, you can just copy and paste my code :)

There are two things we need to achieve for the perfect user experience:

  1. At each scroll download only a small amount of data to display without keeping the user waiting
  2. Show a Loading… message with a progress bar while downloading the data

Let’s define our precious RecyclerView and start downloading the first 8 items.

 

OnScrollListener

This onScrollListener snippet is ready for copy and paste. It takes care of loading new records when the user reached the end of the already downloaded records in the app. This is simpler than what we needed to use with ListViews.

 

Downloading the data

I use Volley to download data from an external source because it’s faster and more efficient than Asynctask. If you don’t use Volley, please take a look at this video to understand how it works. Forget onPreExecute(), doInBackground() and onPostExecute().  Volley is so much easier to use.

Adapter

The key is to use two different ViewHolders. One for the normal items and another one for the footer. We use getItemViewType to determine which one we want to display. Once we downloaded all data from our external database we call mAdapter.hideFooter() to remove the FooterView.

My goal with this tutorial was to show you that creating an endless scroll with Android’s powerful RecyclerView is luckily not so difficult as it first seems to be. I haven’t really found any good tutorials on this so I decided to enjoy suffering for hours and share it with you. I hope it helps!

Need more downloads? Select one of my app promotion services and I will promote your app to 30 quality app review sites and millions of people on social websites.

Feel free to leave a comment or ask a question.

Balint Farago

Entrepreneur, startup enthusiast, gadget fan. I travel a lot and in the meantime I develop and promote mobile apps.

www.scriptsell.net