2

每当我需要更新RecycleView中的警报时,我都会用Im创建一个新的Adapter和一个新的RecycleView。不幸的是它只是一个非常昂贵的“解决方案”我在网上看到我可以使用DiffUti,但我不知道如何实现它。 我创建了一个DiffUtil类:如何使用DiffUtil更新RecyclerView适配器

public class AlarmDiffCallBack extends DiffUtil.Callback{ 
private final ArrayList<SingleAlarm> oldList; 
private final ArrayList<SingleAlarm> newList; 

public AlarmDiffCallBack(ArrayList<SingleAlarm> oldList, ArrayList<SingleAlarm> newList) { 
    this.oldList = oldList; 
    this.newList = newList; 
} 

@Override 
public int getOldListSize() { 
    return oldList.size(); 
} 

@Override 
public int getNewListSize() { 
    return newList.size(); 
} 

@Override 
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { 
    return oldList.get(oldItemPosition).getAlarmIndex() == newList.get(newItemPosition).getAlarmIndex(); 
} 

@Override 
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { 
    return oldList.get(oldItemPosition).getAlarmIndex() == newList.get(newItemPosition).getAlarmIndex(); 
} 

@Nullable 
@Override 
public Object getChangePayload(int oldItemPosition, int newItemPosition) { 
    // Implement method if you're going to use ItemAnimator 
    return super.getChangePayload(oldItemPosition, newItemPosition); 
} 

}

,这是我想更新在recycleView(与DiffUtil取代 “creatAdapter”)的地方之一:

public void add_alarm(final Context context) { 
    //creating a new alarm and set the relevant varible to the addAlarm function 
    button_add_alarm.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //the calndar is without the right day, the day is added in the handler(int the loop) 
      Calendar calendarForAlarm = timePickerToCalendar(alarmTimePicker); 
      alarmsList = alarmHandler.add_alarm(context, checkBoxes, alarmMessage, alarmsList, alarm_manager, calendarForAlarm, repeatAlarm); 
      alarmsList=alarmHandler.sortAlarms(alarmsList); 
      creatAdapt(); 
     } 
    }); 
} 

我只是不知道如何使用这个类来更新我的适配器。我对android和编程一般很陌生,希望这个引用是好的。谢谢!

回答

0

所以基本上你需要你的适配器有一个方法来更新你的数据,这样的:

public void setNewAlarms(List<SingleAlarm> newAlarms){ 
    // oldAlarms is the list of items currently displayed by the adapter 
    AlarmDiffCallBack diffCallBack = new AlarmDiffCallBack(oldAlarms, newAlarms); 

    // Second parameter is to detect "movement". If your list is always sorted the same way, you can set it to false to optimize 
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallBack, true); 

    // This will notify the adapter of what is new data, and will animate/update it for you ("this" being the adapter) 
    diffResult.dispatchUpdatesTo(this); 
} 

然后你只需要调用myAdapater.setNewAlarms(alarmsList)从适配器外,用新的数据。

你也可以看一看实现它

+0

你帮我获得更接近的解决方案,但它仍然没有工作对我来说..首先, ,我看到在你的仓库中手动更新你的列表(list.clear(); list.addall(); - 我应该添加那些列表,即使我操作diffResult.dispatchUpdatesTo(this)? 此外,使用这些函数和没有,它只是剂量更新我的RecyclerView,也许我的AlarmDiffCallBack蠕虫? 非常感谢! –

0

areItemsTheSameareContentsTheSame方法相同的following repository

getChangePayload只被触发,如果areItemsTheSame返回true,areContentsTheSame返回false,所以我觉得它永远不会触发

相关问题