2

Im utilisé pour créer un nouvel adaptateur et un nouveau RecycleView chaque fois que je devais mettre à jour les alarmes dans RecycleView. Malheureusement, c'est juste une "solution" très coûteuse. J'ai vu en ligne que je peux utiliser DiffUti, mais je ne sais pas comment l'implémenter. J'ai créé une classe DiffUtil:Comment mettre à jour l'adaptateur RecyclerView à l'aide de DiffUtil

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); 
} 

}

et c'est l'un des endroits que je veux mettre à jour le recycleView en (remplacer le "creatAdapter" avec le DiffUtil):

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(); 
     } 
    }); 
} 

Je ne peux pas comprendre comment utiliser cette classe pour mettre à jour mon adaptateur. Im assez nouveau pour Android et la programmation en général, espérons que cette citation est OK. Merci!

Répondre

0

Donc, fondamentalement, vous avez besoin de votre adaptateur pour avoir une méthode pour mettre à jour vos données, comme ça:

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); 
} 

Et puis il vous suffit d'appeler myAdapater.setNewAlarms(alarmsList) de l'extérieur de l'adaptateur, avec les nouvelles données.

Vous pouvez également jeter un oeil à la following repository qui l'implémente

+0

Vous avez aidé moi cependant beaucoup plus proche de la solution, il fonctionne encore ne pour moi .. D'abord , J'ai vu que dans votre référentiel vous mettez à jour votre liste manuellement (list.clear(); list.addall(); - devrais-je ajouter ces liste même si j'exécute diffResult.dispatchUpdatesTo (this)? En outre, avec ces fonction et sans, il suffit de mettre à jour mon RecyclerView, peut-être mon AlarmDiffCallBack porté? Merci beaucoup! –

0

Votre areItemsTheSame et areContentsTheSame méthodes sont les mêmes.

getChangePayload ne se déclenche si areItemsTheSame retourne true et areContentsTheSame retourne false, donc je pense que ce ne sera jamais déclencher