You have an infinite number of items or a large number of items to show in a RecyclerView. But Loading all that data at once is neither a good practice nor gives good user experiences. So in this tutorial, we will learn how to implement dynamic loading of data in RecyclerView i.e loading a small amount of data at a time and loading more number of items as a user approaches the end of the previous items list. We will also learn how to show a ProgressBar at the end of RecyclerView while the next items load.
- You should be using Android Studio for development.
- As this tutorial is about loading data dynamically, you should have a REST API configured which accepts limit and skip as parameters.
- In this tutorial, I shall take an example of Comments Activity i.e I shall be writing code for showing a list of comments in RecyclerView. But you must tweak this code as per your App’s requirement.
STEP 1: Add Dependencies
Add the below-given dependency to the app-level build.gradle file.
I have added the dependency for RecyclerView. If you are using CardView in RecyclerView, then you must add a dependency for CardView also.
STEP 2: Define Layout for Item Views inside RecyclerView
Before defining the Adapter class for RecyclerView, we shall define the layout files for the views inside the RecyclerView i.e one layout file for the comment view (view for the item shown in RecyclerView) and another layout file for the ProgressBar that is shown while more data loads at the bottom of RecyclerView.
Copy the below-given code in their respective layout files named comment_layout.xml and progressbar_bottom_layout.xml
STEP 3: Define the POJO class for the Items in RecyclerView
Here, we are defining the POJO (model) class for the items shown inside RecyclerView. Here, it is a class for comment. But you should define it according to your needs.
Copy the below-given code in a file named Comment.java
STEP 4: Define an Interface
In this step, we shall define an Interface which will be used to load more data in the RecyclerView. This interface will contain a method called onLoadMore(). If you don’t know, an Interface contains only abstract methods i.e method without a body. The body of the method is defined where the Interface is implemented.
Copy the below-given code in a file named OnLoadMoreListener.java
STEP 5: Define the Adapter Class
In this step, we will write code for the Adapter class of RecyclerView. I have well commented the code for your understanding along with explanation after the code.
Copy the below-given code in a file named RecyclerViewAdapter.java
In the above code, the main logic lies in the method getItemViewType(). This method matches each item against the VIEW_ITEM except for the last item in the list which is null. This null item is matched against a VIEW_PROG.
Now every time a holder of type ProgressViewHolder is encountered inside the method onBindViewHolder(),onLoadMore() method of the mOnLoadMoreListener is called. This initiates an action inside the Activity class to load more Data.
One more method that needs to be discussed from the above code is removeLastItem(). This method removes the current ProgressBar from the list, when more data has been loaded. This method is called inside Activity class as you will see.
STEP 6: Define Layout for Activity
In this step, we shall define the layout for our Activity which contains RecyclerView.
Copy the below-given code in a file named activity_recycler_view.xml.
STEP 7: Define the Activity Class
This is the last step in which we are going to define our Activity class which contains the RecyclerView. The Activity will handle all the task of loading data dynamically and show in RecyclerView with the help of RecyclerViewAdapter
Copy the below-given code in a file named RecyclerViewActivity.java
- You must tweak above code as per your requirements. I have not used a REST API in above code but you must use a REST API if you want to load data dynamically.
- I have well commented the code above so that you can understand it easily. The logic lies in the OnLoadMoreListener() passed to the Adapter and the null item added to the list.
- I have tried to provide the logic for loading data dynamically in RecyclerView. Now it depends on you, how you want to use it?
- In Case you get stuck, fork the Github repository below for complete code.
If you face any problem in this tutorial, you can always leave a comment below or you can reach me through the chat option visible in the bottom-right corner of the window.