RecyclerView Optimization – Increase Scrolling performance

Tram Ho


Hello friends, when we reschedule, we need to research something. So, let’s find out about RecyclerView today. This is sure all Android dev tools know, so try to optimize it together!

When we implement RecyclerView in our Android Apps, sometimes, we encounter problems like: The RecyclerView scroll item is not smooth. This leads to a bad user experience as it seems our Android Apps are lagging.

Let’s see what we can do to improve the performance of RecyclerView and we will scroll smoother.


RecyclerView optimization techniques

Use the Image-Loading Library

When Garbage Collection (GC) runs on main thread, one of the reasons why UI unresponsive is constant memory allocation and allocation, which leads to running GC very often. By using the bitmap pool concept, we can avoid it.

It is best to use Image-Loading libraries like Glide, Fresco uses this bitmap pool concept. So always use Image-Loading libraries. Authorize all image-related tasks to these libraries.

Set Image Width and Height

If our image width and height are dynamic (not fixed) and we are getting the imageUrl from the server.

If we don’t set the correct image width and height before, the UI will blink during image download and install the image into the ImageView (which actually shows the image when the download is complete).

So we should ask our backend developer to submit the image size or aspect ratio, accordingly, we can calculate the required ImageView width and height.

Then we will only be able to pre-order the width and the height. Hence do not flicker. The problem has been resolved!

Do less in the onBindViewHolder method

Our onBindViewHolder method should do less work. We should test our onBindViewHolder method and optimize it. We can see an improvement in our RecyclerView by doing so.

using the Notify Item RecyclerView API

Whenever we have use cases of removing, updating, adding items, let’s use the notify item API.

Also, check out DiffUtil.

In case we are forced to use InformDataSetChanged () based on our use case, we can try setHasStableIds (true).

It indicates whether each item in the data set can be represented with a unique identifier of type Long.

Even when we call the notifyDataSetChanged () function, it doesn’t have to handle the entire adapter reordering as it can find out if the item is in the same position as before and do less work.

Avoid nested views

If possible, we should avoid nested views and try to create flat views wherever possible. Nested reduces RecyclerView performance. flat view improves performance.

Use setHasFixedSize

We should use this method if the height of all items is equal. Add below and check the performance.

Use the optimal Nested RecyclerView setRecycledViewPool

As we know RecyclerView works on principle “Reuse view using pool” if possible.

If we have a use-case of Nested RecyclerView.

But by default, optimization works for that particular RecyclerView because that particular RecyclerView has its own View Pool.

Pool is not shared between two RecyclerViews with the same view.

So what we can do is we can create a single ViewPool and pass it to all the RecyclerView inside so it is shared like below:

This will improve scroll performance as it will re-start the view from the shared ViewPool.

Use setItemViewCacheSize

We can experiment by setting ItemView’s Cache Size.

According to the official documentation: It sets the amount of off-screen views to keep before adding them to a potentially shared recycled view pool. The offscreen view’s cache is always aware of changes in attached adapters, allowing LayoutManager to reuse unmodified views without going back to the adapter to rebind them.

This means that when we scroll RecyclerView so that there is a view that is almost not completely off screen, RecyclerView will keep it around so we can scroll it back to view without having to call onBindViewHolder () again.

Generally we don’t resize the View Cache, but experiment with it, if it works for you, then implement it.

These are the things we can do to improve the performance of RecyclerView. That’s it for now.


Thank you for following up here. Hope you guys can optimize your RecycledView. If I am missing something, welcome your comments below to let me know! Have a nice day

Share the news now

Source : Viblo