Tips for professional use of RecyclerView. Part 1
- Transfer
I decided to write this article because noted that many developers make mistakes when using RecyclerView
, even though Google has released it for quite some time.
The points described here were mentioned in various reports and materials on Google Devs.
In this article, I will briefly describe the key points from this video , and you do not have to watch it right now. But I recommend watching it in its entirety after reading the article.
1. setHasFixedSize attribute
Set the attribute recyclerView.setHasFixedSize(true)
when you recyclerView
do not plan to resize your children dynamically.
As a result, recyclerView
it will not be redrawn every time the data is updated in the list item, this item is redrawn itself.
2. Click listener
Install the click handler in onCreateViewHolder(...)
.
Whenever a user clicks on a list item, he viewHolder
tells the adapter position where this click occurred ( vh.getAdapterPosition()
). This is important because the elements can be moved inside the adapter, and the associated view
components will not be recreated.
As a result, by the time the view
-component is created, the following may happen: the list item will be, say, at position 2, but when the user clicks on it, the element will already be at position 5. Thus, using the method vh.getAdapterPosition()
ensures that the correct index list.
3. Using various types of view components
Return directly layout
when using different types of view
components (for example, R.layout.view_one
).
If your adapter supports various types view
-components, the method getItemViewType
and onCreateViewHolder
will look something like the image below. You need to write an operator switch
inside the method onCreateViewHolder
to implement the necessary logic for the corresponding types of view
components.
But instead of these types, you can return immediately layout
. This will save you from boilerplate code in onCreateViewHolder
:
This technique cannot be used constantly, because sometimes you may need more complex logic inside each one selected layout
for different cases. But if this is not your case, then the returned ones layout
are the right way to work with various types of view
components.
4. DiffUtil
Use DiffUtil
to add new data to RecyclerView
.
Whenever data recyclerView
changes, most developers call a method notifyDataSetChanged()
to display updated data on a UI. They simply do not know that this method is resource-intensive, and that it is here that DiffUtil
copes much more efficiently.
DiffUtil
- This is a utility class that can calculate the difference between two lists in the form of a list of updates, which then converts the first list to the second. It can be used to calculate updates in the adapter recyclerView
. To use DiffUtil
, you must implement DiffUtil.Callback
, in which there are several required methods required to implement the logic DiffUtil
:
The biggest advantage DiffUtil
is that in RecyclerView
you can update a specific text in a TextView
specific element, instead of redrawing the entire list. To do this, you need to implement method onChangePayload
c DiffUtil.Callback
. There is a very good article on this subject.
In the second part, we will consider other tips for quality use RecyclerView
.