Created
September 30, 2016 01:21
-
-
Save reidbaker/b757093f946caa64e3fa79f2f8701649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
10/10 would compare lists with