13

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

2
  • What do you mean the same item? If the item is the ViewHolder, then yes that is the way the recycler works. If you mean one entry from your data set then that doesn't seem right.
    – Sam
    Jan 26, 2018 at 21:54
  • @Sam By 'item' i mean one ViewHolder. So you say, this is the normal behavior of the RecyclerView. I dont get the point, why it is called again, if the viewHolder was already created for this item without a change to the dataset?
    – Palm
    Jan 26, 2018 at 21:59

1 Answer 1

19

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.