Updated 25 March 2021
This blog covers how we can Improve Recycler View Performance. Recently, I’ve been working on an application that has a vertical list in which every item has again a vertical/grid list. I faced recycle view performance problem. It didn’t scroll smoothly. When I scrolled it, it took time to render the items & I faced issue in smooth scrolling.
When we scroll down/up recycleview, the item which moved out from the visible screen becomes scrapped view. For loading the next coming item on screen, recycleview uses scrapped views collection. The view which loaded from scrapped views collection is called dirty view. This selected view is recycled and shown on screen.
Suppose, we have list of 100 items. 5 items can be shown on screen at a time. Currently, we are showing 11-15 items.
When we scroll down, item 12 to 16 will be shown on screen and item 11 will be moved out from visible screen. This item 11 will become scrapped view. If we again scroll down, then item 12 will also become scrapped view and items 13-17 will be shown on screen. We have collection of scrap views: item 11 & 12.
Now for item 18, one of scrap view item will be selected & recycled to show on the screen. This selected item will be called dirty view.
When we scroll items from top to bottom, tell recycleview to load offscreen items in advance. Through which, we can save view recycling time while scrolling & scrolling will become smooth.
We can create our custom LinearLayoutManager & set setExtraLayoutSpace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class CustomLayoutManager : LinearLayoutManager { private var extraLayoutSpace = 1000 // you can change it according to your items & screen size private var context: Context? = null constructor(context: Context?) : super(context) { this.context = context } constructor(context: Context, extraLayoutSpace: Int) : super(context) { this.context = context this.extraLayoutSpace = extraLayoutSpace } constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super( context, orientation, reverseLayout ) { this.context = context } fun setExtraLayoutSpace(extraLayoutSpace: Int) { this.extraLayoutSpace = extraLayoutSpace } override fun getExtraLayoutSpace(state: RecyclerView.State): Int { return extraLayoutSpace } } |
Set the number of offscreen views to retain before adding them to the potentially shared recycled view pool.
Suppose, currently item 41-45 are showing on screen. When we scroll bottom to top, if old item will remain in cache then they will be shown easily on screen without any recycling of views or we can say without calling onBindViewHolder(). So for smooth scrolling, we can increase the cache size.
1 |
recycleview.setItemViewCacheSize(100) |
Through these two solution, we can Improve Recycler View Performance.
To learn more about recyclview, please check following link : Recycle View
Hope, this blog will be helpful to you.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.