Skip to content

Instantly share code, notes, and snippets.

@smarteist smarteist/DiffUtilCallback
Last active May 24, 2019

Embed
What would you like to do?
It's more simple callback for calculating the diff between two non-null items in a list.
/**
* It's more simple callback for calculating the diff between two non-null items in a list.
* <p>
* {@link DiffUtil.Callback} serves two roles - list indexing, and item diffing. ItemCallback handles
* just the second of these, which allows separation of code that indexes into an array or List
* from the presentation-layer and content specific diffing code.
*
* @param <T> Type of items to compare.
*/
public abstract class DiffUtilCallback<T> extends DiffUtil.Callback {
private List<T> oldItemsList;
private List<T> newItemsList;
public final DiffUtil.DiffResult calculateDiff(List<T> oldItems, List<T> newItems, boolean detectMoves) {
this.oldItemsList = oldItems;
this.newItemsList = newItems;
return DiffUtil.calculateDiff(this, detectMoves);
}
public final DiffUtil.DiffResult calculateDiff(List<T> oldItems, List<T> newItems) {
this.oldItemsList = oldItems;
this.newItemsList = newItems;
return DiffUtil.calculateDiff(this);
}
@Override
public final int getOldListSize() {
return oldItemsList.size();
}
@Override
public final int getNewListSize() {
return newItemsList.size();
}
@Override
public final boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return areItemsTheSame(oldItemsList.get(oldItemPosition), newItemsList.get(newItemPosition));
}
@Override
public final boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return areContentsTheSame(oldItemsList.get(oldItemPosition), newItemsList.get(newItemPosition));
}
public abstract boolean areItemsTheSame(T oldItem, T newItem);
public abstract boolean areContentsTheSame(T oldItem, T newItem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.