Register now for Android Dev Summit 2019!

ItemCallback

abstract class ItemCallback<T : Any!>
kotlin.Any
   ↳ androidx.recyclerview.widget.DiffUtil.ItemCallback

Callback for calculating the diff between two non-null items in a list.

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.

Summary

Public constructors

Callback for calculating the diff between two non-null items in a list.

Public methods

abstract Boolean
areContentsTheSame(@NonNull oldItem: T, @NonNull newItem: T)

Called to check whether two items have the same data.

abstract Boolean
areItemsTheSame(@NonNull oldItem: T, @NonNull newItem: T)

Called to check whether two objects represent the same item.

open Any?
getChangePayload(@NonNull oldItem: T, @NonNull newItem: T)

When areItemsTheSame(T, T) returns true for two items and areContentsTheSame(T, T) returns false for them, this method is called to get a payload about the change.

Public constructors

<init>

ItemCallback()

Callback for calculating the diff between two non-null items in a list.

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.

Public methods

areContentsTheSame

abstract fun areContentsTheSame(@NonNull oldItem: T, @NonNull newItem: T): Boolean

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 Object#equals(Object) so that you can change its behavior depending on your UI.

For example, if you are using DiffUtil with a RecyclerView.Adapter, you should return whether the items' visual representations are the same.

This method is called only if areItemsTheSame(T, T) returns true for these items.

Note: Two null items are assumed to represent the same contents. This callback will not be invoked for this case.

Parameters
oldItem T: The item in the old list.
newItem T: The item in the new list.
Return
Boolean: True if the contents of the items are the same or false if they are different.

areItemsTheSame

abstract fun areItemsTheSame(@NonNull oldItem: T, @NonNull newItem: T): Boolean

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: null items in the list are assumed to be the same as another null item and are assumed to not be the same as a non-null item. This callback will not be invoked for either of those cases.

Parameters
oldItem T: The item in the old list.
newItem T: The item in the new list.
Return
Boolean: True if the two items represent the same object or false if they are different.

getChangePayload

@Nullable open fun getChangePayload(@NonNull oldItem: T, @NonNull newItem: T): Any?

When areItemsTheSame(T, T) returns true for two items and areContentsTheSame(T, T) returns false for them, this method is called to get a payload about the change.

For example, if you are using DiffUtil with RecyclerView, you can return the particular field that changed in the item and your ItemAnimator can use that information to run the correct animation.

Default implementation returns null.