Tips for professional use of RecyclerView. Part 1

Original author: Arif Khan
  • Transfer

Tips for professional use of RecyclerView.  Part 1

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 recyclerViewdo not plan to resize your children dynamically.

As a result, recyclerViewit 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 viewHoldertells the adapter position where this click occurred ( vh.getAdapterPosition()). This is important because the elements can be moved inside the adapter, and the associated viewcomponents 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 layoutwhen using different types of viewcomponents (for example, R.layout.view_one).

If your adapter supports various types view-components, the method getItemViewTypeand onCreateViewHolderwill look something like the image below. You need to write an operator switchinside the method onCreateViewHolderto implement the necessary logic for the corresponding types of viewcomponents.

Standard getItemViewType and onCreateViewHolder

But instead of these types, you can return immediately layout. This will save you from boilerplate code in onCreateViewHolder:

Uploaded onCreateViewHolder

This technique cannot be used constantly, because sometimes you may need more complex logic inside each one selected layoutfor different cases. But if this is not your case, then the returned ones layoutare the right way to work with various types of viewcomponents.

4. DiffUtil

Use DiffUtilto add new data to RecyclerView.

Whenever data recyclerViewchanges, 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 DiffUtilcopes 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:

Public Methods DiffUtil.Callback

The biggest advantage DiffUtilis that in RecyclerViewyou can update a specific text in a TextViewspecific element, instead of redrawing the entire list. To do this, you need to implement method onChangePayloadc DiffUtil.Callback. There is a very good article on this subject.

In the second part, we will consider other tips for quality use RecyclerView.

→ Tips for professional use of RecyclerView. Part 2

Also popular now: