

Cleaning up resources in RecyclerView ViewHolder
source link: https://androidexplained.github.io/android/ui/2020/11/09/cleaning-up-resources-in-view-holder.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Cleaning up resources in RecyclerView ViewHolder
Nov 9, 2020
About 4 mins
In your journey you might have often wondered when exactly to clean up resources in a RecyclerView ViewHolder.
The topic isn’t easy and like most you probably tried to use onViewAttachedToWindow & onViewDetachedFromWindow, only to find it’s not working and gave up hoping you’ll get away with
no resource freeing mechanism.
Sure there is this 1% of users of your app who get OutofMemoryException from time to time and on older phones scrolling through a list of items looks more like playing Tetris.
You didn’t really care until you get a negative review with 500 upvotes saying that your app is almost unusable.
But worry not, in this article we will look at how to cleanup resources in RecyclerView ViewHolder properly.
Release the… resources
There are couple of methods that will be important to us in this article
- RecyclerView.Adapter#onBindViewHolder - called to display data at specified position, should update the contents of ViewHolder to reflect an item at position
- RecyclerView.Adapter#onViewRecycled - called when View is recycled and put into the pool, since the View might sit in the pool for a while it might be a good idea to release heavy resources like bitmaps in this method
Lets look at a situation when user is scrolling downwards in a RecyclerView.
We will assume that there are two ViewHolders of the same type displayed (1 & 2).
- items are displayed on the screen - onBindViewHolder will be called for both ViewHolder 1 & 2
- user scrolls down and the top ViewHolder 1 disappears, it will be put into the pool, after which the onViewRecycled method will be called on that ViewHolder
- at the same time new ViewHolder appears on the bottom - since we have ViewHolder 1 in the pool it will be reused and onBindViewHolder will be called again to reflect the new position
- and so on and so on…
Therefore we can basically utilize these two above methods to initialise resources (onBindViewHolder) and cleanup them up (onViewRecycled) accordingly.
The below code can serve as an example of how this would look in practice
class MovieAdapter(private val movies: Array<String>) :
RecyclerView.Adapter<MovieAdapter.ViewHolder>() {
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) =
viewHolder.onBind(movies[position])
override fun getItemCount() = movies.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.movie_item, parent, false)
return ViewHolder(view)
}
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
holder.cleanup()
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun onBind(movie: String) {
// initialise resources and adjust the view to reflect position in the RecyclerView
}
fun cleanup() {
// release heavy resources like bitmaps etc.
}
}
}
In this example we could for instance download a movie cover image and display it in the onBindViewHolder after which we would release the downloaded Bitmap in onViewRecycled method to cleanup this heavy object so it does not sit unused in the pool.
In this article we have learned how to clean up resources in RecyclerView.ViewHolder!
Recommend
-
354
Android开发:RecyclerView平滑流畅的滑动到指定位置 2017年10月17日 12:14 · 阅读 12105 ...
-
89
Attention. This project is not maintained any more !!! vlayout 中文文档 Projects of Tangram Android Project
-
110
AutoAdapter This Repository simplifies working with RecyclerView Adapter Gradle: Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://ji...
-
90
RecyclerViewCardGallery 声明:代码fork自 https://github.com/huazhiyuan2008/RecyclerViewCardGallery/ 感谢原作者 RecyclerView实现循环ba...
-
37
README.md
-
20
README.md
-
51
本文字数: 1569 字 预计阅读时间: 5分钟 1.前言 在App的开...
-
19
去年8月份,我在Github开源了一个无限循环的轮播图库— BannerViewPager (以下简称BVP)。时至今日,一年多的时间过去了,BVP在大家的支持下已经在GitHub上收获了1.9k的Star。...
-
8
使用Databinding为Recyclerview使用同一个ViewHolder加载不同Item 提示:在阅读本篇文章前,你最好对android databinding有一定了解,本文使用的代码均为kotlin,但是不用担心,都很简单 最近在写项目的时候使用了databinding技术,...
-
7
RecyclerView中隐藏部分ViewHolder导致的Scroll计算问题 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK