The RecyclerView is a UI component that allows us to create a scrollable list. It was introduced in Android Lollipop and it is a kind of ListView2.
If we can, we should use it instead of ListView, because it forces us to use a more efficient way to implement a list and separates areas of responsibility between classes. To implement the RecyclerView, we need to create the following components:
RecyclerView, which we should add to our screen layout;
layout for each line of the list;
an adapter that contains data and associates it with a list.
Android programming for beginners
Want to upgrade to Android, but don't know where to find quality material? Take some free lessons from our course and get full access with a 50% discount.
How do I create a RecyclerView?
Before we do that, we need to add the dependency to the build.gradle file.
dependencies {
implementation 'com.android.support:recyclerview-v7:27.0.0'
}
Don't forget to update your library to the latest version. You can check it out here. We can now add the RecyclerView to layout.
Adding RecyclerView to layout activity_main.xlm:
Adding RecyclerView to layout activity_main.xlm
Creating RecyclerView in Activity:
Creating RecyclerView in Activity
In the created RecyclerView instance, we have to set the LayoutManager. Following the documentation:
The LayoutManager is responsible for positioning the views in the RecyclerView, as well as determining when to reuse views that are no longer visible to the user.
Thus, this means that the LayoutManager displays the list in a specific form. In this example, we used the LinearLayoutManager, which shows data in a simple list - vertical or horizontal (vertical by default). But this can be changed very easily by using a different LayoutManager. For example GridLayoutManager, StaggeredGridLayoutManager or WearableLinearLayoutManager. Great, now we will create a layout for each row of the list.
How do I create a layout for a list row?
Just like any other layout.
Creating a layout for a list row
The layout of the list row is pretty simple - some image and text next to it. To do this, we used a horizontal LinearLayout. Pay attention to the warning on LinearLayout. It tells us that we need to design our layout in a more efficient way.
Android Studio warning message
In this case, we could have used the android: drawableStart attribute on the TextView, but then the image would have to be properly sized. On the other hand, the ImageView gives me more options and I was able to resize the image easily - I wanted to keep it as simple as possible. The android: padding attribute is used in LinearLayout. If you are not familiar with this attribute, you can take a look at my short article about it, Now we have to create an adapter.
How do I create an adapter?
Example of RecyclerView adapter:
RecyclerView adapter example
We have to create a class that extends RecyclerView.Adapter, which takes as a parameter a class that extends RecyclerView.ViewHolder. Also some required methods need to be overridden. What do these methods do?
getItemCount () returns the total number of items in the list. List values are passed using the constructor.
onCreateViewHolder () creates a new ViewHolder object whenever the RecyclerView needs it. This is when the list row layout is created, passed to the ViewHolder object, and each child view component can be found and saved.
onBindViewHolder () takes a ViewHolder object and sets the required data for the corresponding row in the view component. The whole code looks like this.
An example of RecyclerView implementation:
RecyclerView implementation example
We can run the project and see the result.
We can run the project and see the result
Why do we need the ViewHolder pattern?
In short, the ViewHolder prevents unnecessary calls to findViewById (). If you are not familiar with this pattern, I recommend that you take a look at my example of implementing a list with a ListView. I believe this is the best way to find out what this pattern is for and what potential problems it solves. If a list row layout is not created for every row, then how many times is it actually created? How many times are the onCreateViewHolder () and onBindViewHolder () methods called?
How does RecyclerView reuse views?
To test this, we will put some logs in our code.
As you can see, the onCreateViewHolder () method is called and the ViewHolder objects are created for the first few views, and then they are reused, and the adapter just binds the data using the onBindViewHolder () method. This makes the list very efficient and the user can scroll the list smoothly, because on and more difficult operations (creating and searching for elements of view components) occur inside the onCreateViewHolder () method. That's all!