Open In App

How to Improve RecyclerView Scrolling Performance in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

RecyclerView is the main UI component in Android which is used to represent the huge list of data. If the RecyclerView is not implemented properly then it will not smoothly scrollable and it may lead to a bad user experience. To make it scrollable smoothly we have to optimize it and follow some tips to improve its performance. In this article, we will see the steps to follow for optimizing the Scrolling performance of our RecyclerView.  

Note:

If you are looking for the implementation guide of RecyclerView. Then do check out: How to implement RecyclerView in Android

1. Set a specific width and height to ImageView in RecyclerView items

In RecyclerView if you’re using ImageView to display an image from your server in your RecyclerView items then specify the constant size for your ImageView. If the size of ImageView in RecyclerView items is not fixed then RecyclerView will take some time to load and adjust the RecyclerView item size according to the size of Image. So to improve the performance of our RecyclerView we should keep the size of our ImageView to make it RecyclerView load faster. 

2. Avoid using NestedView

While creating an item for RecyclerView avoid using NestedView for RecyclerView item. Nesting will reduce the performance of RecyclerView. So it is better to avoid using Nested View. Nested View means adding a Horizontal RecyclerView in a Vertical RecyclerView. This type of Nesting may reduce RecyclerView performance. Below is the image of Nested RecyclerView. 

3. Use the setHasFixedsize method

If the height of our RecyclerView items is fixed then we should use the setHasFixedsize method in our XML of our card item. This will fix the height of our RecyclerView item and prevent it from increasing or decreasing the size of our Card Layout. We can use this method in our JAVA class as well where we are declaring our RecyclerView adapter and layout manager.

recyclerView.setHasFixedSize(true)

4. Use the image loading library for loading images

RecyclerView is composed of so many images and if you are loading an image in all cards then loading each image from the server will take so much time. As the Garbage Collection runs on the main thread so this is the reason which results in unresponsive UI. Continuous allocation and deallocation of memory lead to the frequent GC run. This can be prevented by using the bitmap pool concept. The simple solution to tackle this problem is to use Image Loading libraries such as Picasso, Glide, Fresco, and many more. These libraries will handle all bitmap pool concept and all delegate tasks related to images. 

Note: You may also refer to the Top 5 Image Loading Libraries in Android

5. Do less work in the OnBindViewHolder method

OnBindViewHolder method is used to set data in each item of RecyclerView. This method will set the data in each RecyclerView item. We should do less work in this method. We only have to set data in this method and not to do any comparison or any heavy task. If we perform any heavy task in this method then this will degrade the performance of our RecyclerView because setting data for each item will take a specific amount of time. So to improve the performance of RecyclerView we should do less work in the OnBindViewHolder method.

6. Use the NotifyItem method for your RecyclerView

Whenever you are performing actions in RecyclerView such as adding an item in RecyclerView at any position or deleting an item from a specific position of RecyclerView then you should use NotifyItemChange() method. 

Java




// below method will notify the adapter 
// of our RecyclerView when an item 
// from RecyclerView is removed. 
adapter.notifyItemRemoved(position)
    
// below method will be used when
// we update the data of any RecyclerView 
// item at a fixed position.  
adapter.notifyItemChanged(position)
    
// below method is used when we add 
// any item at a specific position
// of our RecyclerView.  
adapter.notifyItemInserted(position)
    
// below method is used when we add multiple 
// number of items in our recyclerview.
// start represents the position from 
// which the items are added.
// end represents the position upto 
// which we have to add items.   
adapter.notifyItemRangeInserted(start, end)


When we will call the notifyDataSetChanged() method it will not handle the complete reordering of the RecyclerView adapter but it will find if the item at that position is the same as before to do less work. 



Last Updated : 21 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads