# DiffUtil.Callback

# DiffUtil.Callback

Artifact:[androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview)  
[View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/DiffUtil.java+class:androidx.recyclerview.widget.DiffUtil.Callback)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil.Callback "View this page in Java")

<br />

```
abstract class DiffUtil.Callback
```

<br />

*** ** * ** ***

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

## Summary

|                                                 ### Public constructors                                                  |
|--------------------------------------------------------------------------------------------------------------------------|
| [Callback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#Callback())`()` |

|                                      ### Public functions                                      |
|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `abstract `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areContentsTheSame(int,int))`(oldItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Called by the DiffUtil when it wants to check whether two items have the same data.                                                                                                                                                                                                                                                                                                                         |
| `abstract `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areItemsTheSame(int,int))`(oldItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Called by the DiffUtil to decide whether two object represent the same Item.                                                                                                                                                                                                                                                                                                                                      |
| [Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?`                 | [getChangePayload](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#getChangePayload(int,int))`(oldItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` When[areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areItemsTheSame(int,int))returns`true`for two items and[areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areContentsTheSame(int,int))returns false for them, DiffUtil calls this method to get a payload about the change. |
| `abstract `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | [getNewListSize](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#getNewListSize())`()` Returns the size of the new list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `abstract `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | [getOldListSize](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#getOldListSize())`()` Returns the size of the old list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |

## Public constructors

### Callback

Added in 1.0.0  

```
Callback()
```  

## Public functions

### areContentsTheSame

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
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[equals](https://developer.android.com/reference/java/lang/Object.html#equals(java.lang.Object))so that you can change its behavior depending on your UI. For example, if you are using DiffUtil with a[RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter), you should return whether the items' visual representations are the same.

This method is called only if[areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areItemsTheSame(int,int))returns`true`for these items.  

|                                           Parameters                                           |
|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| `oldItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the old list                            |
| `newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the new list which replaces the oldItem |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if the contents of the items are the same or false if they are different. |

### areItemsTheSame

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the old list |
| `newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the new list |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if the two items represent the same object or false if they are different. |

### getChangePayload

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

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

When[areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areItemsTheSame(int,int))returns`true`for two items and[areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/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](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView), you can return the particular field that changed in the item and your[ItemAnimator](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ItemAnimator)can use that information to run the correct animation.

Default implementation returns`null`.  

|                                           Parameters                                           |
|------------------------------------------------------------------------------------------------|------------------------------------------|
| `oldItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the old list |
| `newItemPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position of the item in the new list |

|                                    Returns                                     |
|--------------------------------------------------------------------------------|--------------------------------------------------------------------|
| [Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?` | A payload object that represents the change between the two items. |

### getNewListSize

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
abstract fun getNewListSize(): Int
```

Returns the size of the new list.  

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The size of the new list. |

### getOldListSize

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
abstract fun getOldListSize(): Int
```

Returns the size of the old list.  

|                                   Returns                                   |
|-----------------------------------------------------------------------------|---------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The size of the old list. |