Android RecyclerView Load More on Scroll with Example
Many apps display so many amounts of data in the form of a list. This data is so much so that it cannot be loaded at a time. If we load this data at a time then it may take so much loading time and degrades the performance of our RecyclerView. So to solve this we generally load the data in chunks and display it at a time. In this article, we will take a look at loading this data by showing a ProgressBar and load data in the infinite list.
What we are going to build in this article?
We will be building a simple application in which we will be displaying the infinite list and we will load the data with the help of the ProgressBar below. We will be calling the same method to add data to our ArrayList. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the below dependency in your build.gradle file and allow internet permission in the manifests file
Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section. We have used the Picasso dependency for image loading from the URL.
// below line is used for volley library implementation ‘com.android.volley:volley:1.1.1’ // below line is used for image loading library implementation ‘com.squareup.picasso:picasso:2.71828’
Add internet permission in the manifests file.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 4: Working with Modal Class
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as CourseModal and add the below code to it.
Step 5: Creating a layout file for our item of RecyclerView
Navigate to the app > res > layout > Right-click on it > New > Layout Resource File and name it as course_rv_item and add the below code to it.
Step 6: Creating an adapter class for setting our data
Similarly, create another Java class and name it as CourseRVAdapter and add the below code to it. Comments are added in the code to get to know in more detail.
Step 7: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.