简体   繁体   中英

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!

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 : " Recycler View"

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.

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.

RecyclerView.ViewHolder is a helper class that holds the View of a row or rows. One or more ViewHolder is created for each viewType.

if several rows have the same ViewType then the same View can be reused for several rows.

Overriding getItemViewType(int position) is the way to have several view types. If getItemViewType returns a not used before viewType then onCreateViewHolder will be called to create a new ViewHolder.

Adapter onBindViewHolder is the place to fill the view with specific data for each row.

ADDED:

A concept must be clear: what makes a ViewHolder to be reused is that it shares the same viewType. Instead if you make getItemViewType(int position) return a different value for each row then each row will have its independent ViewHolder and view.

assume that you want to show a list of 1000 items and there are just 10 items visible to the user in the screen. your adapter creates 10 ViewHolder instances to show them at the same time. when user scrolls and the adapter has to show more items, instead of creating new instances of ViewHolder, it reuses items that are not visible anymore. your adapter prevents creating new Views and saves CPU time by doing so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM