Partial update of RecyclerView.ViewHolder

前端 未结 1 1012
余生分开走
余生分开走 2020-12-28 14:05

I\'m trying to change the field with time in my RecyclerView. Each individual ViewHolder contains a CardView and a few more views insi

相关标签:
1条回答
  • 2020-12-28 14:56

    You can notify your RecyclerView.Adapter's observers to issue a partial update of your RecyclerView.ViewHolders by passing a payload Object.

    notifyItemRangeChanged(positionStart, itemCount, payload);
    

    Where payload could be or contain a flag that represents relative or absolute time. To bind the payload to your view holders, override the following onBindViewHolder(viewHolder, position, payloads) method in your adapter, and check the payloads parameter for data.

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            // Perform a full update
            onBindViewHolder(viewHolder, position);
        } else {
            // Perform a partial update
            for (Object payload : payloads) {
                if (payload instanceof TimeFormatPayload) {
                    viewHolder.bindTimePayload((TimeFormatPayload) payload);
                }
            }
        }
    }
    

    Within your MyViewHolder.bindTimePayload(payload) method, update your time TextViews to the time format specified in the payload.

    0 讨论(0)
提交回复
热议问题