In this post I included an easy approach to implement the Endless Load more with loading progress in RecyclerView. I made this project as simple as possible, The main view lists movie names with IMDB rating (loads from server). When you reach the bottom by scrolling down the list, app will load more movies by indicating a loading progress.
– PHP Source added
For Network Requests I used Retrofit/OkHttp in this project. Even the project included Retrofit, I will skip that part in this post as this post mean to RecyclerView load more implementation. Full source code available at Github you can download and use the code as you wish.
RecyclerView is the Advanced and Flexible version of ListView which handles large datasets efficiently by maintaining a limited number of views (Like the ViewHolder Pattern used in ListView ). You can use the same RecyclerView for list and Grid type views by the setting the RecyclerView.LayoutManager.
As we are implementing RecyclerView Load More I assume you have created the project and have enough knowledge on RecyclerView layout and required dependencies. Else read here to get the basic understanding.
OK Let’s start … Below is the final RecyclerView adapter class I used in this project look at code, I will explain the required blocks separately.
– It has two View Types (Movie[title,rating] and Loading Progress)
– MovieModel a data model class which have title,rating and type
– boolean isLoading is used to set the loading and load completed status. Which will prevent unnecessary the back to back load more call.
– you should call the custom method notifyDataChanged() to handle the isLoading status
– boolean isMoreDataAvailable is used to set the server data availablity status. Which will prevent app from requesting when no more data available at the server. Call adapter.setMoreDataAvailable(false); to notify the adapter that there is no more data available at the server.
– interface OnLoadMoreListener has a callback method onLoadMore which will get called whenever the scroll reached at the bottom.
Below is the example code block I used to call the load more in Activity class.
– In main activity implements OnLoadMoreListener() to get callback when scroll reaches the bottom.
– In loadMore() remote request method I adds loading progress view to the bottom of list by setting the MovieModel type to load.
– In data response callback removes the added load type from the MovieModel list, then adds the server response to the current list. When server returns blank array sets adapter.setMoreDataAvailable(false) to prevent further requests.
That’s it… For further reference checkout the full source code at GitHub.