Skip to content

Most visited

Recently visited

navigation
added in version 25.1.0
belongs to Maven artifact com.android.support:recyclerview-v7:27.0.0

DiffUtil.Callback

public static abstract class DiffUtil.Callback
extends Object

java.lang.Object
   ↳ android.support.v7.util.DiffUtil.Callback


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

Summary

Public constructors

DiffUtil.Callback()

Public methods

abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition)

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

abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition)

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

Object getChangePayload(int oldItemPosition, int newItemPosition)

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 int getNewListSize()

Returns the size of the new list.

abstract int getOldListSize()

Returns the size of the old list.

Inherited methods

From class java.lang.Object

Public constructors

DiffUtil.Callback

added in version 25.1.0
DiffUtil.Callback ()

Public methods

areContentsTheSame

added in version 25.1.0
boolean areContentsTheSame (int oldItemPosition, 
                int newItemPosition)

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 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

Returns
boolean True if the contents of the items are the same or false if they are different.

areItemsTheSame

added in version 25.1.0
boolean areItemsTheSame (int oldItemPosition, 
                int newItemPosition)

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

Returns
boolean True if the two items represent the same object or false if they are different.

getChangePayload

added in version 25.1.0
Object getChangePayload (int oldItemPosition, 
                int newItemPosition)

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

Returns
Object A payload object that represents the change between the two items.

getNewListSize

added in version 25.1.0
int getNewListSize ()

Returns the size of the new list.

Returns
int The size of the new list.

getOldListSize

added in version 25.1.0
int getOldListSize ()

Returns the size of the old list.

Returns
int The size of the old list.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)