Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active May 24, 2019 07:02
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 smarteist/e445acb707b21da48f2ac91b895051a4 to your computer and use it in GitHub Desktop.
Save smarteist/e445acb707b21da48f2ac91b895051a4 to your computer and use it in GitHub Desktop.
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