Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created May 9, 2018 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anitaa1990/4726169dc26ee5d3d6298b1bb1e523b8 to your computer and use it in GitHub Desktop.
Save anitaa1990/4726169dc26ee5d3d6298b1bb1e523b8 to your computer and use it in GitHub Desktop.
Sample gist from sample app - https://github.com/anitaa1990/RoomDb-Sample
public class NoteDiffUtil extends DiffUtil.Callback {
List<Note> oldNoteList;
List<Note> newNoteList;
public NoteDiffUtil(List<Note> oldNoteList, List<Note> newNoteList) {
this.oldNoteList = oldNoteList;
this.newNoteList = newNoteList;
}
@Override
public int getOldListSize() {
return oldNoteList.size();
}
@Override
public int getNewListSize() {
return newNoteList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldNoteList.get(oldItemPosition).getId() == newNoteList.get(newItemPosition).getId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldNoteList.get(oldItemPosition).equals(newNoteList.get(newItemPosition));
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
//you can return particular field for changed item.
return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment