Recyclerview History
Recently I discovered this old talk from Google I/O 2009 and in the first 14 minutes they explain the Recyclerview, the Adapter, and the Viewholder incredibly well. Here's a few important takeaways from this video, although I'd recommend watching it in its entirety:
1. The Adapter takes in data and generates Views via a function Adapter.getView()
that takes in a position, a View, a parent ViewGroup, and returns a View.
2. The naive solution to implement Adapter.getView()
would be to use the position to get the right data and then just inflate a new View that belongs to the parent ViewGroup.
3. The first optimization is that we can avoid inflating new views by just recycling old views when they leave the visible area. We would use the position, the View (recycled when it is not visible) and just bind new data into that existing View.
4. The second optimization is that we can avoid doing new findViewById()
calls by storing all the referenced Views in a class called a ViewHolder, and storing this ViewHolder as a tag to the View.
However, I wonder if this ViewHolder optimization is still useful these days, since Kotlin Android Extensions includes caching for the referenced Views. Also, many people who use view binding will store just a reference to the parent binding but will not actually store a reference to each individual View that they would need to access. I wonder in this case how much performance benefit would ViewHolder actually offer...
Comments
Post a Comment