Skip to content

Instantly share code, notes, and snippets.

@reidbaker
Created September 30, 2016 01:21
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 reidbaker/b757093f946caa64e3fa79f2f8701649 to your computer and use it in GitHub Desktop.
Save reidbaker/b757093f946caa64e3fa79f2f8701649 to your computer and use it in GitHub Desktop.
private void updateList(@NonNull List<Device> newDevices) {
// Sort new list
Collections.sort(newDevices, mComplicatedComparitor);
//Calculate difference between old and new list
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new CustomDiffCallback(mDevices, newDevices));
//Wipe old list and replace with sorted new list
mDevices.clear();
mDevices.addAll(newDevices);
//Let diff handle ui update calls to adapter
result.dispatchUpdatesTo(this);
}
private static class CustomDiffCallback extends DiffUtil.Callback {
final List<Device> mOldDevices;
final List<Device> mNewDevices;
public SecurityDeviceDiffCallback(@NonNull List<Device> oldDevices, @NonNull List<Device> newDevices) {
mOldDevices = oldDevices;
mNewDevices = newDevices;
}
@Override
public int getOldListSize() {
return mOldDevices.size();
}
@Override
public int getNewListSize() {
return mNewDevices.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mOldDevices.get(oldItemPosition).getId().equals(
mNewDevices.get(newItemPosition).getId());
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return false;
}
}
@atommarvel
Copy link

10/10 would compare lists with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment