[英]How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it
I am populating new data in my RecyclerView adapter all at once, so there are no insert or remove one item actions.我一次在 RecyclerView 适配器中填充新数据,因此没有插入或删除一项操作。
So simply, i have an old list and when some Event occurs i get the new list and i can assign the new list to the old.很简单,我有一个旧列表,当发生某些事件时,我得到新列表,我可以将新列表分配给旧列表。
Problems are i cannot make properly the animation for each item in the old list问题是我无法为旧列表中的每个项目正确制作动画
Here is something i have now, which i thought will work for first case - item move to new position:这是我现在拥有的东西,我认为它适用于第一种情况 - 项目移动到新位置:
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
However it does not work as expected, and there is difference between logs(which are correct) and the list appearing on the phone(with wrong items positions, some duplicates, buggy etc.)但是它没有按预期工作,并且日志(正确的)和手机上出现的列表(项目位置错误,一些重复,马车等)之间存在差异。
So how can i make it work?那么我怎样才能让它工作呢? With notifyItemMoved, notifyItemInserted and notifyItemRemoved?
使用notifyItemMoved、notifyItemInserted 和notifyItemRemoved?
I don't want to just use NofifyDataSetChanged, because it refresh the entire list instead of just updating the items with animations that have changed.我不想只使用 NofifyDataSetChanged,因为它会刷新整个列表,而不仅仅是使用已更改的动画更新项目。
It looks like that your new data is also a form of list, not a single item.看起来您的新数据也是一种列表形式,而不是单个项目。 I think this could be a good candidate for using DiffUtil in the support library.
我认为这可能是在支持库中使用DiffUtil的不错选择。 Here is also a nice tutorial for it.
这也是一个很好的教程。
It will allow you to calculate the difference in the new data and only update needed fields.它将允许您计算新数据中的差异并仅更新所需的字段。 It will also offload the work asynchronously.
它还将异步卸载工作。
You just need to implement a DiffUtil.Callback
to indicate if your items are the same or the contents are the same.您只需要实现一个
DiffUtil.Callback
来指示您的项目是否相同或内容是否相同。
You update your recyclerView like that:你像这样更新你的 recyclerView :
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);
Simply use DiffUtil like简单地使用 DiffUtil 就像
final MyDiffCallback diffCallback = new MyDiffCallback(prevList, newList); final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
Create a Callback by extending MyDiffCallback and override methods and do as needed.通过扩展 MyDiffCallback 和覆盖方法创建回调并根据需要执行。
public class MyDiffCallback extends DiffUtil.Callback
// override methods // 覆盖方法
Well for this ,I feel this would be the easiest.Just follow it ->好吧,我觉得这将是最简单的。只要按照它 ->
Replace this替换这个
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
with和
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyDataSetChanged();
new CountDownTimer(250, 250) {
@Override
public void onTick(long millisUntilFinished) {
Log.d("millisUntilFinished", "" + millisUntilFinished);
}
@Override
public void onFinish() {
notifyItemMoved(i, j);
}
}.start();
}
}
}
}
}
this will update the values and after 250 millisecond(1/4th a second),the value with be moved with animation.这将更新值,并在 250 毫秒(每秒 1/4 秒)后,随着动画移动该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.