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
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.
By default it have 5. you can increase as per your need. Save this answer.
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).
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With