Callback

abstract class Callback
kotlin.Any
   ↳ androidx.recyclerview.widget.DiffUtil.Callback

A Callback class used by DiffUtil while calculating the diff between two lists.

Summary

Public constructors

Public methods
abstract Boolean
areItemsTheSame(oldItemPosition: Int, newItemPosition: Int)

Called by the DiffUtil to decide whether two object represent the same Item.

abstract Int

Returns the size of the old list.

abstract Int

Returns the size of the new list.

open Any?
getChangePayload(oldItemPosition: Int, newItemPosition: Int)

When #areItemsTheSame(int, int) returns true for two items and #areContentsTheSame(int, int) returns false for them, DiffUtil calls this method to get a payload about the change.

abstract Boolean
areContentsTheSame(oldItemPosition: Int, newItemPosition: Int)

Called by the DiffUtil when it wants to check whether two items have the same data.

Public constructors

<init>

Callback()

Public methods

areItemsTheSame

abstract fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean

Called by the DiffUtil to decide whether two object represent the same Item.

For example, if your items have unique ids, this method should check their id equality.

Parameters
oldItemPosition Int: The position of the item in the old list
newItemPosition Int: The position of the item in the new list
Return
Boolean: True if the two items represent the same object or false if they are different.

getOldListSize

abstract fun getOldListSize(): Int

Returns the size of the old list.

Return
Int: The size of the old list.

getNewListSize

abstract fun getNewListSize(): Int

Returns the size of the new list.

Return
Int: The size of the new list.

getChangePayload

open fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any?

When #areItemsTheSame(int, int) returns true for two items and #areContentsTheSame(int, int) returns false for them, DiffUtil calls this method 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.

Parameters
oldItemPosition Int: The position of the item in the old list
newItemPosition Int: The position of the item in the new list
Return
Any?: A payload object that represents the change between the two items.

areContentsTheSame

abstract fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean

Called by the DiffUtil when it wants to check whether two items have the same data. DiffUtil uses this information to detect if the contents of an item has changed.

DiffUtil uses 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(int, int) returns true for these items.

Parameters
oldItemPosition Int: The position of the item in the old list
newItemPosition Int: The position of the item in the new list which replaces the oldItem
Return
Boolean: True if the contents of the items are the same or false if they are different.