Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does notifyItemChanged() force a call to onBindViewHolder()?

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).

like image 593
Dale_Plante Avatar asked Jun 24 '15 12:06

Dale_Plante


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.

What is notifyItemChanged in android?

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.

What does notifyDataSetChanged do in RecyclerView?

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.

What is onCreateViewHolder?

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 .


1 Answers

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.

like image 111
vishal arote Avatar answered Oct 11 '22 08:10

vishal arote