How to Use SnapHelper in RecyclerView in Android?
SnapHelper is an amazing feature that is seen in RecyclerView. With the help of this feature, we can make the items of the RecyclerView properly visible. This feature of Recycler View is present in most of the apps, but it is not visible. This feature is generally seen in the Google Play application in which when we scroll the horizontal recycler view, we will get to see only a specific number of recycler view items visible. When we use normal recycler view we can get to see while scrolling some items are half present in the device screen. So to solve this issue in Android Recycler View. We have to use Snap Helper in our Recycler View. In this article, we will take a look at implementing this RecyclerView in our app.
What is SnapHelper?
SnapHelper is a helper class that is used to snap any child of our RecyclerView. With the help of this class, we can display the specific number of RecyclerView items on our screen, and we can avoid the RecyclerView children’s display inside our RecyclerView. This feature is generally seen in RecyclerView which is used in Google Play Store.
What we are going to build in this article?
We will be building a simple application in which we will be displaying the list of courses in our RecyclerView. In this RecyclerView we will be implementing the SnapHelper class feature to avoid the elements partially present in the user’s device. Below is the video in which we will get to see what we are going to build 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: 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 3: Creating a modal class for storing our data
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. Comments are added in the code to get to know in more detail.
Step 4: Creating a new 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 below code to it.
Step 5: Creating an adapter class
Navigate to the app > java > your app’s package name > Right-click on it > New > 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 6: 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.