Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why payloads in RecyclerView' onBindViewHolder is a list?

 public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
        onBindViewHolder(holder, position);
 }

I know when we want to update some view not all in RecyclerView item, i can use

public final void notifyItemChanged(int position, Object payload) {
        mObservable.notifyItemRangeChanged(position, 1, payload);
}

As that code see, the param is a object, but why in Adapter it change to list, and i must use list.get(0) to find my payload ?

Thanks

like image 524
wanbo Avatar asked Oct 15 '17 10:10

wanbo


People also ask

What is the use of onBindViewHolder?

onBindViewHolder. Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

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.

How many times onCreateViewHolder is called?

By default it have 5. you can increase as per your need. Save this answer.

What does notifyDataSetChanged do in RecyclerView?

What does notifyDataSetChanged() do on recyclerview ? 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.


1 Answers

From Android Docs:

Partial bind vs full bind:

The payloads parameter is a merge list from notifyItemChanged(int, Object) or notifyItemRangeChanged(int, int, Object). If the payloads list is not empty, the ViewHolder is currently bound to old data and Adapter may run an efficient partial update using the payload info. If the payload is empty, Adapter must run a full bind. Adapter should not assume that the payload passed in notify methods will be received by onBindViewHolder(). For example when the view is not attached to the screen, the payload in notifyItemChange() will be simply dropped.

It's a list because it's a merge list. You could have potentially called notifyItemChanged multiple times before the view is updated, each time with a potentially different payload.

For example, at the same time, multiple threads could simultaneously request an item update with payloads "fav count update" and "icon change" and "time stamp update". So it's not wise to assume your payload is the 0th item.

like image 157
user1032613 Avatar answered Sep 29 '22 16:09

user1032613