Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding RecyclerView.ViewHolder

I'm having hard time understanding working of view holder, here are my some question that could increase my understanding of viewholder:

It is said that oncreateViewHolder returns viewholder object, What is viewholder object does it contain all the views in single row? if there is list of 1000 item how many viewobjects will be created?

My understanding: If we are creating viewholder object it contains reference of view like findviewbyid, since findviewbyid is expansive operation, so by viewholder we can create single viewholder object and reuse by just setting image or text(happens in onBindView).

But onCreateViewHolder runs multiple times and as a result findviewbyid will also execute multiple time, isn't performance issue?

Also how its different from convertView of base adapter of simple listview

Thanks!

like image 991
blackHawk Avatar asked Aug 06 '17 16:08

blackHawk


People also ask

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.

What is ViewHolder pattern?

The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

What is the relationship between RecyclerView adapter and RecyclerView ViewHolder?

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView . A class that extends ViewHolder that will be used by the adapter.

What is ViewHolder in Kotlin?

This class is deprecated. A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView.


2 Answers

View holder it is thing which helps u you to reduce find view by id calls. Let give you an example.

Suppose you have 1k items, each have a 5 view you need to find by id and only 5 full items can be shown once at screen.

So, recyclerView will create 7 (5 + one not-full bottom and one not-full top) view holders. Next time when recyclerView will be scrolled it will use existing viewHolders. Exactly as name says : "RecyclerView"

So findViewById will be called 7*5=35 times. If you do not use viewHolder you would get 5*1000 = 5000 calls.

35 vs 5000, so you understood I think.

like image 129
Valentun Avatar answered Oct 25 '22 11:10

Valentun


It is said that oncreateViewHolder returns viewholder object, What is viewholder object does it contain all the views in single row? if there is list of 1000 item how many viewobjects will be created?

One ViewHolder object for one view row. One ViewHolder object is created for every time the onCreateViewHolder is called. It is called based on the number of visible items in the device. Even if you have 100 items, if ony 10 items are visible, the onCreateViewHolder will be called 10 times and there will be 10 ViewHolders. (There might be one or two extra item based on the RecyclerView optimizations because if you scroll the list, the next item should be visible instantaneously)

My understanding: If we are creating viewholder object it contains reference of view like findviewbyid, since findviewbyid is expansive operation, so by viewholder we can create single viewholder object and reuse by just setting image or text(happens in onBindView).

RecyclerView is already recycling and reusing the Views and the corresponding ViewHolders. The number of ViewHolder (and View) present at any time depends on the number visible items on the screen.

But onCreateViewHolder runs multiple times and as a result findviewbyid will also execute multiple time, isn't performance issue?

As said previously, the number of times this will be called is only for the number of visible items. When you scroll, the views and viewholders are reused. You have distinct Views for each row. So there will be distinct ViewHolder for each row.

Also how its different from convertView of base adapter of simple listview

In ListView, the convertView is the old view, which provides an option to reuse the same view for new rows as you scroll the list. But it's optional because the developer might not use the convertView at all. In RecyclerView the reusing of old views is done automatically.

like image 20
Bob Avatar answered Oct 25 '22 09:10

Bob