Recyclerview. Adapter has a function with payloads, which is ignored by many people because it is not abstract. This function is defined as follows:
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
onBindViewHolder(holder, position);
}
Copy the code
The default implementation of this function calls the abstract function onBindViewHolder(holder, position).
The payloads parameter is a List object that is not null but may be empty. The payload object can be set by using the Adapter’s notifyXXX function with the payload parameter. For example, the function notifies an entry of data changes:
public final void notifyItemChanged(int position, Object payload) {
mObservable.notifyItemRangeChanged(position, 1, payload);
}
Copy the code
So let’s see what this payload is.
When notifyItemChanged is called, the RecyclerView calls onBindViewHolder(holder, position) to update the View of the current data change. But for more complex entries, there are many different controls, such as pictures, text, CheckBox, etc., the user clicks on the current entry (such as like a microblog message), need to highlight the status of the like. That is, there is currently only one favorite state change in a tweet entry, but the contents of all views need to be set again in onBindViewHolder(holder, Position). For each View, setting its contents triggers the View to be rearranged and repositioned, so that at least one View changes state and the entire item needs to be rearranged.
So if you tell the system that this is a tweet, it’s a payload, The payloads in calling onBindViewHolder(VH holder, int Position, List payloads) contain the data you specify. And then you can change only the views that have changed, not the other views. Thus, the rendering efficiency of RecyclerView can be improved.
It is a time when the payloads change, so you can call a function with no payloads at that time.
@Override public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {if (payloads. IsEmpty ()) {// payloads are empty, OnBindViewHolder (holder, position); } else {// payloads are not empty, this only updates the View that needs to be updated. holder.mBadgeView.setVisibility(((Item)payloads.get(0)).disabled ? View.VISIBLE : View.INVISIBLE); }}Copy the code