-
public abstract class DiffUtil.ItemCallback<T>
Callback for calculating the diff between two non-null items in a list.
Callback serves two roles - list indexing, and item diffing. ItemCallback handlesjust the second of these, which allows separation of code that indexes into an array or Listfrom the presentation-layer and content specific diffing code.
-
-
Method Summary
Modifier and Type Method Description abstract boolean
areItemsTheSame(@NonNull() T oldItem, @NonNull() T newItem)
Called to check whether two objects represent the same item. abstract boolean
areContentsTheSame(@NonNull() T oldItem, @NonNull() T newItem)
Called to check whether two items have the same data. Object
getChangePayload(@NonNull() T oldItem, @NonNull() T newItem)
When areItemsTheSame returns {@code true}
for two items and areContentsTheSame returns false for them, this method is called toget a payload about the change.-
-
Method Detail
-
areItemsTheSame
abstract boolean areItemsTheSame(@NonNull() T oldItem, @NonNull() T newItem)
Called to check whether two objects represent the same item.
For example, if your items have unique ids, this method should check their id equality.
Note:
{@code null}
items in the list are assumed to be the same as another{@code null}
item and are assumed to not be the same as a non-{@code null}
item. This callback willnot be invoked for either of those cases.- Parameters:
oldItem
- The item in the old list.newItem
- The item in the new list.
-
areContentsTheSame
abstract boolean areContentsTheSame(@NonNull() T oldItem, @NonNull() T newItem)
Called to check whether two items have the same data.
This information is used to detect if the contents of an item have changed.
This method to check equality instead of equals so that you canchange its behavior depending on your UI.
For example, if you are using DiffUtil with a RecyclerView.Adapter, you shouldreturn whether the items' visual representations are the same.
This method is called only if areItemsTheSame returns
{@code true}
forthese items.Note: Two
{@code null}
items are assumed to represent the same contents. This callbackwill not be invoked for this case.- Parameters:
oldItem
- The item in the old list.newItem
- The item in the new list.
-
getChangePayload
@Nullable() Object getChangePayload(@NonNull() T oldItem, @NonNull() T newItem)
When areItemsTheSame returns
{@code true}
for two items and areContentsTheSame returns false for them, this method is called toget a payload about the change.For example, if you are using DiffUtil with RecyclerView, you can return theparticular field that changed in the item and your ItemAnimator can use thatinformation to run the correct animation.
Default implementation returns
{@code null}
.
-
-
-
-