问题描述
我正尝试在 RecyclerView
中随时间更改字段。每个单独的 ViewHolder
都包含一个 CardView
和其他一些视图。我要制作动画的唯一视图是带有时间的视图。如您所见,没有动画:
I'm trying to change the field with time in my RecyclerView
. Each individual ViewHolder
contains a CardView
and a few more views inside. The only view I want to animate is the one with time. As you can see, there is no animation:
adapter.notifyDataSetChanged();
一次更新项目无济于事,因为整个 CardView
都会闪烁:
Updating items one by one doesn't help because then the whole CardView
flashes:
int len = adapter.getItemCount();
for(int i=0;i<len;i++) {
adapter.notifyItemChanged(i);
}
是否可以获取所有 ViewHolders
的列表,然后更新(动画)一个 TextView
放在每个里面?
Is there a way to get a list of all ViewHolders
to then update (animate) just that one TextView
inside each one?
推荐答案
您可以通知您的 RecyclerView.Adapter
的观察者通过传递有效负载 Object
来发布您的 RecyclerView.ViewHolders
的部分更新
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);
有效载荷
可能是或包含标志的位置代表相对或绝对时间。要将有效载荷
绑定到视图持有者,请在适配器中重写以下 onBindViewHolder(viewHolder,position,payloads)
方法,然后检查有效载荷
参数以获取数据。
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);
}
}
}
}
在您的 MyViewHolder.bindTimePayload(payload)
方法,将您的时间 TextViews
更新为<$ c $中指定的时间格式c>有效载荷。
Within your MyViewHolder.bindTimePayload(payload)
method, update your time TextViews
to the time format specified in the payload
.
这篇关于RecyclerView.ViewHolder的部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!