RecyclerView onBindViewHolder called multiple times for one item

Palm picture Palm · Jan 26, 2018 · Viewed 8.2k times · Source

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

Answer

Abbas picture Abbas · Jan 26, 2018

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.