Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView onBindViewHolder called multiple times for one item

I have used the RececlerView with the ViewHolder pattern for a while now. Im am implementing a custom Adapter.

Im am not searching for a specific bug help in my code.

I was just wondering, if it's normal, that the onBindViewHolder method is called multiple times (for the same item) while scrolling to the end of the list and scrolling back up. In this case onBindViewHolder is called again for item 0 and 1 (the list contains 7 items in total)

Is there any possibility for this method to get called AGAIN without notifying that the datasat has changed?

Im a bit confused.

Kind Regards, Palm

like image 901
Palm Avatar asked Jan 26 '18 21:01

Palm


People also ask

How often is onBindViewHolder called?

However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable.

How many times onCreateViewHolder is called?

By default it have 5. you can increase as per your need. Save this answer.

Is there an addHeaderView equivalent for RecyclerView?

addHeaderView() but you can achieve this by adding a type to your adapter for header. everything seems to be OK and it should work, but however make the recycler view MATCH_PARENT see if anything changes. also if its possible make the recycler view the root of that layout (its good for performance).

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. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.


1 Answers

Yes it is perfectly normal for a RecyclerView to call onBindViewHolder() multiple times.

A RecyclerView only creates minimum number of Views needed to fill the screen. And it works by reusing the old/created Views. So that when you are scrolling down the View that hid during the scrolling to the top is removed and brought next to the last visible View and added there. But since the View is currently bound with old data onBindViewHolder() is called again to ensure that the View is bound with only the correct data before it is rendered.

Similarly you'll notice that onCreateViewHolder() is only called the exact minimum number of Views it needs.

For a better understanding of how the RecyclerView works I suggest you read up on Recycler, LayoutManager and Recycler.Adapter the three core parts of a RecyclerView.

like image 77
Abbas Avatar answered Sep 28 '22 23:09

Abbas