public abstract class

DiffUtil.Callback

extends java.lang.Object

 java.lang.Object

↳androidx.recyclerview.widget.DiffUtil.Callback

Overview

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

Summary

Constructors
publicCallback()

Methods
public abstract booleanareContentsTheSame(int oldItemPosition, int newItemPosition)

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

public abstract booleanareItemsTheSame(int oldItemPosition, int newItemPosition)

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

public java.lang.ObjectgetChangePayload(int oldItemPosition, int newItemPosition)

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

public abstract intgetNewListSize()

Returns the size of the new list.

public abstract intgetOldListSize()

Returns the size of the old list.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Callback()

Methods

public abstract int getOldListSize()

Returns the size of the old list.

Returns:

The size of the old list.

public abstract int getNewListSize()

Returns the size of the new list.

Returns:

The size of the new list.

public abstract 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: The position of the item in the old list
newItemPosition: The position of the item in the new list

Returns:

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

public abstract 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 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 DiffUtil.Callback.areItemsTheSame(int, int) returns true for these items.

Parameters:

oldItemPosition: The position of the item in the old list
newItemPosition: The position of the item in the new list which replaces the oldItem

Returns:

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

public java.lang.Object getChangePayload(int oldItemPosition, int newItemPosition)

When DiffUtil.Callback.areItemsTheSame(int, int) returns true for two items and DiffUtil.Callback.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: The position of the item in the old list
newItemPosition: The position of the item in the new list

Returns:

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