Three years ago I wrote short article about Endless ListView in Android. This solution was dedicated to
ListView, wasn’t perfect and generic. Moreover, lot of people asked questions about this solution and they weren’t sure how to use it properly. I also had some problems with implementing endless scroll AKA infinite scroll in Android apps. In addition, in the newest Android SDK it’s recommended to use
RecyclerView instead of
ListView. For a long time I couldn’t find the right implementation of the infinite scroll AKA endless scroll for Android. A few solutions I’ve found weren’t production ready, weren’t working correctly or had too many features. I wanted to have small, easy and flexible solution to implement infinite scroll for Android, which works with
RecyclerView from the newest Android API. That’s why I created new project called InfiniteScroll, which helps to implement infinite scroll on Android. Library is tested and its behavior is documented by appropriate unit tests. If you would like to see it in action, check out gif animation presenting how sample app works. Library is very tiny, but does its job.
How to use it?
First of all, we need to add library dependency to our
We also need to create necessary fields in our
Next, we need to create new
Then, we need to initialize
LinearLayoutManager in our
Activity and add previously created listener to
If we want to display loading progress, we should add additional view for it, show it while loading starts and hide it when loading is finished.
Please note: We can also implement “Load more” button with that library. Instead of loading items immediately, we can display such button when user scrolled to the end of the list.
You can see how, library can be implemented in the following places:
- Sample app from main repository, which loads Strings into
- Search Twitter app, which allows to search tweets and scroll them infinitely
I think, this time infinite scroll is implemented in the right and re-usable way. I hope, it will make your life easier while developing your apps.
Source code of the library can be found at: https://github.com/pwittchen/InfiniteScroll.