How to Implement RecyclerView | Guide for Android Developers
All the useful knowledge about implementing a RecyclerView in one place. Learn how to set a Layout Manager, how to create an adapter, set the data on the list and much more!
Do you want to learn how to implement RecyclerView and create a list with RecyclerView? You are in the right place! In this article, you will learn:
- how to create the RecyclerView
- how to set a LayoutManager
- how to create an adapter
- how to set the data on the list
- why we need the ViewHolder pattern
What is RecyclerView in Android?
RecyclerView is a UI component that allows us to create a scrolling list. It was introduced with Android Lollipop and it’s a kind of ListView2. If we are able to, we should use this one instead of a ListView because it forces us to use a more efficient way of implementing a list and separates responsibilities to other classes.
If you aren’t familiar with the ListView, check out my article about how to implement a ListView in your Android project. What’s more, here you can go deeper into the differences between ListView and RecyclerView.
It’s worth learning how to implement RecyclerView because it is widely used nowadays – every company providing Android app development services uses RecyclerView in their projects. At Droids On Roids, we use RecyclerView in many projects in all kinds of applications e.g. in health & fitness app development.
Now, let’s go deeper into creating a list with RecyclerView.
To implement a RecyclerView, we have to create the following:
- a RecyclerView which we should add to our screen layout,
- a layout for each row in the list,
- an adapter that holds the data and binds them to the list.
How to create a RecyclerView
Before we do that, we have to add a dependency to the build.gradle.
Remember to update the library version to the most recent one. You can check it here.
Now we are able to add a RecyclerView to the layout.
On the created RecyclerView instance, we have to set a Layout Manager. Following this documentation:
A LayoutManager is responsible for measuring and positioning item views within a RecyclerView, as well as determining the policy for when to recycle item views that are no longer visible to the user.
So, basically, it means that the LayoutManager is layouting the list in the chosen way. In this example, we’ve used LinearLayoutManager which shows the data in a simple list — vertically or horizontally (by default vertically). But it can be changed in very simply, just by calling a different one, like GridLayoutManager, StaggeredGridLayoutManager or event WearableLinearLayoutManager. Great, now we’ll create a layout for each list row.
How to create a list row layout
In the same way as any other layout.
The list row layout is pretty simple – it just needs some image and text next to it. To make this happen, we used LinearLayout with a horizontal orientation. Note the warning on the LinearLayout. It’s telling us that we should design our layout in a more efficient way.
In this case, we could use the
android:drawableStart attribute in TextView but then the image should also have the proper size. ImageView, on the other hand, gives me more possibilities and I was able to change the image size easily – I wanted to keep it as simple as I can.
android:padding attribute is used in the LinearLayout. If you aren’t familiar with this attribute, you should take a look at my short article about it.
Now, we should create an adapter.
How to create an adapter
We have to create a class which extends RecyclerView.Adapter, which as a parameter takes a class which extends RecyclerView.ViewHolder. Another thing that we need to do is override some needed methods.
What are these methods doing?
getItemCount()returns the total number of the list size. The list values are passed by the constructor.
onCreateViewHolder()creates a new ViewHolder object whenever the RecyclerView needs a new one. This is the moment when the row layout is inflated, passed to the ViewHolder object and each child view can be found and stored.
onBindViewHolder()takes the ViewHolder object and sets the proper list data for the particular row on the views inside.
The whole code in one piece looks like this:
We can build the project and see the results.
Why we need the ViewHolder pattern
Long story short, the ViewHolder prevents unnecessary
findViewById() calls on each bind. If you aren’t familiar with this pattern, I encourage you to look at my example of list implementation using ListView. I believe that this is the best way to know what is going on with this pattern and what the potential problems can occur from not using it.
If the list row layout isn’t inflated for each row, how many times is it inflated? How many times are the
onCreateViewHolder() and the
onBindViewHolder() method called?
How RecyclerView reuses views
To check this, we’ll put some logs in our code.
Ok, so let’s see what is happening.
As you can see, the
onCreateViewHolder() method is called and the ViewHolder objects are created for a couple of first views and then the created ones are being reused, while the adapter is just binding the data with the
onBindViewHolder()method. Thanks to this, the list is very efficient and a user can scroll the list smoothly because the most costly calls (like inflating and finding views) are happening inside the
A RecyclerView is a bit more complicated type of View but it’s very common to use on our daily basis, so we should know how to handle it. We’ve learned how to create the RecyclerView and set a LayoutManager, how to create an adapter, set the data on the list and why we need the ViewHolder pattern. I hope you liked it, and now you know how to implement a RecyclerView in your Android project. All the best!