When I call notifyItemChanged()
on my RecyclerView
, the corresponding Views
(in this case, buttons) do not get updated as expected. The reason I am calling them is that data has changed which would cause their background color to change. Does this mean that notifyItemChanged(
) doesn't force a call to onBindViewHolder()
?
I tried using notifyDataSetChanged()
and that DOES cause the views to reload as I want, so I know my implementation is correct. (Note I don't want to use notifyDataSetChanged()
, as that is inefficient, I only used it as a sanity test).
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.
notifyItemChanged. Notify any registered observers that the item at position has changed with an optional payload object. This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated.
notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.
onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .
To Refresh your listview Register a new observer to listen for data changes. using registerAdapterDataObserver
The adapter may publish a variety of events describing specific changes. Not all adapters may support all change types and some may fall back to a generic "something changed" event if more specific data is not available.
Components registering observers with an adapter are responsible for unregistering those observers when finished.
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