The method and RecyclerView notify payload parameter

 

Data updated list, there are usually two update events, one is item change event, change event entry, the data item is changed; one is the structural change event, the event structural changes that insert, delete or move some item.

These changes can be updated separately in RecycleView.Adapter, increasing efficiency, but also a little more convenient. There are multiple methods RecyclerView.Adapter notify the user may have selected use.

In the list display, can be divided into two parts, one is the list, the data is stored information that we have made data to be displayed from the list and then displayed; the other items, is already displayed in the screen entry information. Usually, we go in there onBindViewHolder binding method

One,

RecyclerView.Adapter the notify mainly in the following:

1.

void notifyDataSetChanged ()

notifyDataSetChanged BaseAdapter updating method is the same. Notify the user of the data has changed, but did not specify the content changes, so we have all the data is updated, including item and structural.

2.

void notifyItemChanged (int position) //单形参

void notifyItemChanged (int position, Object payload) // //两个形参

notifyItemChanged belongs to the item change event. Data to inform the user of a particular item has changed, to update after update, index unchanged. For the use of payload, please read on.

The following is equivalent to when the four call:

notifyItemchange(position)    ==   notifyItemchange(position,null)   

==  notifyItemRangechange(position,1)   ==  notifyItemRangechange(position,1,null) 

This is because in the source code, notifyItemchange calling notifyItemRangechange to achieve.

è¿éåå¾çæè¿ °

3.

void notifyItemInserted (int position)

 notifyItemInserted belong to structural change event. This method notifies the user has inserted a new item came in, and by the animation, you can use the default, you can also customize. Is newly inserted item position on the position, and the position from the original starting position subsequent item, index all +1.

Usage is generally as follows:

list.add(i, data);  //list的插入

adapter.notifyItemInserted(i);  //item的插入,但是i后面的item的position没有变

adapter.notifyItemRangeChanged(i, adapter.getItemCount()-i); // 更新从i开始的item的position

Why should refresh position for the second and subsequent data? Because, in the position after position 2 inserted piece of data, the new data into the position 2, and that the original position of 2 should become 3,3 should be turned into a 4, so all data after 2 the position have changed, so it is necessary to position2 future data to be refreshed.

If you used here notifyDataSetChanged () to refresh the display on the screen all the item can do? The results can not go wrong, but there is a problem, call the front notifyItemInserted () method will execute after the animation, if you call notifyDataSetChanged () all item displayed on the screen is refreshed, it certainly will refresh the item currently being executed animation , result of this is that the animation front has not been performed, it is immediately refreshed, animation out of sight. So long as the refresh 2 after item on it.

(This excerpt from Andrews notes Man -RecyclerView in notifyDataSetChanged refresh summary )

4.

void notifyItemMoved (int fromPosition, int toPosition)

 Belonging to the structural change event. For notifying item movement. item moved to the position from fromPosition toPosition position.

Point out, Moved than two data exchange, but which will in turn move in the direction of a position fromPosition toPosition until toPosition moved up. After calling notifyItemMoved method, position item between fromPosition and toPosition all changed. You can refresh the following way:

if (toPosition == fromPosition)return;

notifyItemMoved(fromPosition, toPosition);

notifyItemRangeChanged(Math.min(fromPosition, toPosition), Math.abs(fromPosition - toPosition) +1);

5.

void notifyItemRemoved (int position)

  Belonging to the structural change event. Inform the user on the item at the position location has been deleted. -1 item indices after all.

 Usage is generally:

    public void remove(int position) {
        mDataList.remove(position); //数组长度-1,但是item的长度还没有变

        notifyItemRemoved(position); // 会有删除的动画效果,但是没有调用onBindViewHolder方法没有调用,position没有刷新
       
        notifyItemRangeChanged(position, mDataList.size() - position); //对position进行刷新
       
    }

 6.

void notifyItemRangeChanged (int positionStart, int itemCount, Object payload)

void notifyItemRangeChanged (int positionStart, int itemCount)

 Belonging to the item change event, and updating of a range. From positionStart beginning, there itemCount one item needs to be updated. After the update, the index unchanged. Refresh here is called directly onBindViewHolder method for position each item in the range has been refreshed. For the use of payload, please read on.

The following calls are equivalent:

notifyItemRangechange(position,1)    ==   notifyItemRangechange(position,1,null)   

7.

void notifyItemRangeInserted (int positionStart,  int itemCount)

Belonging to the structural change event, and updating of a range. This method of notifying users itemCount new item inserted entered. Is newly inserted item position on the position, and the position from the original starting position subsequent item, index all + itemCount.

8.

void notifyItemRangeRemoved (int positionStart, int itemCount)

Belonging to the structural change event, and updating of a range. Notify a user itemCount one item has been deleted. item index back all -itemCount.

 

two,

The above method, the conventional method comprising payload parameter. Detailed usage, please refer to cloud Shiqianfeng - RecyclerView improve rendering efficiency using Payload

Simply put, the further refinement of the partial update and improve efficiency.

The above method, when the payload is null, the entire Item are updated, the callback system

void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) 

A method for updating the item; when the payload is not null (as long as the line is not null, may be any value), the system will callback

void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads)

The method, in this method, we can determine whether the payload is empty, there is a choice of any item in the control is updated.

Specific usage is as follows:

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            // payloads 为 空,说明是更新整个 ViewHolder 
            onBindViewHolder(holder, position);
        } else {
            // payloads 不为空,这只更新需要更新的 View 即可。
           ViewHolder viewHolder = (ViewHolder) holder;
           holder.tv.setText("Hello World!");
        }
    }

 

Guess you like

Origin blog.csdn.net/Smile_Qian/article/details/85253003