I am writing this article as I have observed many developers making mistakes when implementing recyclerview even now when it’s been quite a while since Google released it.

The points mentioned here are referenced from various talks and articles by Google Devs.

You don’t need to watch the video now as I will be jotting down all the important points from the video in this article. However, I suggest you watch the whole video at the end.

Set setHasFixedSize attribute recyclerView.setHasFixedSize(true) when the recycler view item do not change sizes of its children at runtime.

By doing this, recyclerview won’t request layout whenever data is updated in the recyclerview item and the view will just invalidate by itself.

2. Set item click listener in onCreateViewHolder(…)

Whenever the user clicks on any item, viewholder gives the adapter position where the click happened ( vh.getAdapterPosition() ). This is important because items can move within the adapter and those views will not be rebound. So by the time it gets bounded, it can be at say, position 2 but when a user clicks it, it may be at position 5. So using vh.getAdapterPosition() ensures returning the correct index

3. When using different view types return layout ids(eg R.layout.view_one)

If your adapter has different view types then getItemViewType and onCreateViewHolder would be something like below. You have to write a switch case statement inside your onCreateViewHolder to inflate respective layouts.

So instead of returning types, you can return the layouts. This will remove the boilerplate code from the onCreateViewHolder like below.

This trick cannot be used always as sometimes you require items inside each inflated layouts for different use cases but if it’s not the case for you, returning ID’s is the way to go to handle different view types.

4. Use DiffUtil to add new data to your recyclerview

Whenever the data in the recyclerview gets changed, most developers call notifyDataSetChanged() to show new data on the UI. What they fail to understand is that it’s a costly operation and that’s exactly where DiffUtil comes to the rescue.

DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one. It can be used to calculate updates for a RecyclerView Adapter. To use DiffUtil you have to implement “DiffUtil.Callback” which has few methods (shown below) that you need to provide the logic for DiffUtil to work

The best thing about DiffUtil is that you can tell your recyclerview to just update the specific text on the textview in a particular item instead of requesting layout for the whole layout but for that, you need to implement the onChangePayload method of the DiffUtil.Callback. There is a very nice article written by Mert Şimşek — https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00

If you liked the article, don’t shy away from giving a clap. It will motivate me to write more articles like this. Thanks

Part 2 — https://proandroiddev.com/recyclerview-pro-tips-part-2-341e6f449a62