# DiffUtil.ItemCallback

# DiffUtil.ItemCallback

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.ItemCallback)  
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.ItemCallback "View this page in Java")

<br />

```
abstract class DiffUtil.ItemCallback<T>
```

<br />

*** ** * ** ***

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

[Callback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.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.  

| Parameters |
|------------|---------------------------|
| `<T>`      | Type of items to compare. |

## Summary

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

|                                      ### 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.ItemCallback#areContentsTheSame(T,T))`(oldItem: T, newItem: T)` Called 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.ItemCallback#areItemsTheSame(T,T))`(oldItem: T, newItem: T)` Called to check whether two objects 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.ItemCallback#getChangePayload(T,T))`(oldItem: T, newItem: T)` When[areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.ItemCallback#areItemsTheSame(T,T))returns`true`for two items and[areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.ItemCallback#areContentsTheSame(T,T))returns false for them, this method is called to get a payload about the change. |

## Public constructors

### ItemCallback

Added in 1.0.0  

```
ItemCallback()
```  

## Public functions

### areContentsTheSame

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

```
abstract fun areContentsTheSame(oldItem: T, 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[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.ItemCallback#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. |

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

|                                                                    See also                                                                     |
|-------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areContentsTheSame(int,int)) |   |

### areItemsTheSame

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

```
abstract fun areItemsTheSame(oldItem: T, 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. |

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

|                                                                 See also                                                                  |
|-------------------------------------------------------------------------------------------------------------------------------------------|---|
| [areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#areItemsTheSame(int,int)) |   |

### getChangePayload

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

```
fun getChangePayload(oldItem: T, newItem: T): Any?
```

When[areItemsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.ItemCallback#areItemsTheSame(T,T))returns`true`for two items and[areContentsTheSame](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.ItemCallback#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](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`.  

|                                                                  See also                                                                   |
|---------------------------------------------------------------------------------------------------------------------------------------------|---|
| [getChangePayload](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.Callback#getChangePayload(int,int)) |   |