# SortedList.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/SortedList.java+class:androidx.recyclerview.widget.SortedList.Callback) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
abstract class SortedList.Callback<T2> : Comparator<T2!>, ListUpdateCallback
```

<br />

Known direct subclasses [SortedList.BatchedCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback), [SortedListAdapterCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedListAdapterCallback)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback` | A callback implementation that can batch notify events dispatched by the SortedList. |
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedListAdapterCallback` | A `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback` implementation that can bind a `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList` to a `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter`. |

*** ** * ** ***

The class that controls the behavior of the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList`.

It defines how items should be sorted and how duplicates should be handled.

SortedList calls the callback methods on this class to notify changes about the underlying data.

## Summary

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

| ### Public functions |
|---|---|
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)(oldItem: T2!, newItem: T2!)` Called by the SortedList when it wants to check whether two items have the same data or not. |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)(item1: T2!, item2: T2!)` Called by the SortedList to decide whether two objects represent the same Item or not. |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#compare(T2,T2)(o1: T2!, o2: T2!)` Similar to `https://developer.android.com/reference/java/util/Comparator.html#compare(T, T)`, should compare two and return how they should be ordered. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#getChangePayload(T2,T2)(item1: T2!, item2: T2!)` When `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)` returns `true` for two items and `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` returns false for them, `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback` calls this method to get a payload about the change. |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, count: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Called by the SortedList when the item at the given position is updated. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int,java.lang.Object)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, count: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, payload: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` Called when count} number of items are updated at the given position. |

### Inherited functions

From [java.util.Comparator](https://developer.android.com/reference/java/util/Comparator.html) `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T, U : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-comparable/index.html<U!>?> https://developer.android.com/reference/java/util/Comparator.html#comparing(java.util.function.Function&lt;? super T,? extends U&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/Function.html<T!, U!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T> https://developer.android.com/reference/java/util/Comparator.html#comparingDouble(java.util.function.ToDoubleFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToDoubleFunction.html<T!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T> https://developer.android.com/reference/java/util/Comparator.html#comparingInt(java.util.function.ToIntFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToIntFunction.html<T!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T> https://developer.android.com/reference/java/util/Comparator.html#comparingLong(java.util.function.ToLongFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToLongFunction.html<T!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-comparable/index.html<T!>?> https://developer.android.com/reference/java/util/Comparator.html#naturalOrder()()` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T> https://developer.android.com/reference/java/util/Comparator.html#nullsFirst(java.util.Comparator&lt;? super T&gt;)(comparator: https://developer.android.com/reference/java/util/Comparator.html<T!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T> https://developer.android.com/reference/java/util/Comparator.html#nullsLast(java.util.Comparator&lt;? super T&gt;)(comparator: https://developer.android.com/reference/java/util/Comparator.html<T!>!)` `java-static https://developer.android.com/reference/java/util/Comparator.html<T!>!` `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-comparable/index.html<T!>?> https://developer.android.com/reference/java/util/Comparator.html#reverseOrder()()` `https://developer.android.com/reference/java/util/Comparator.html<T!>!` `https://developer.android.com/reference/java/util/Comparator.html#reversed()()` `https://developer.android.com/reference/java/util/Comparator.html<T!>!` `https://developer.android.com/reference/java/util/Comparator.html#thenComparing(java.util.Comparator&lt;? super T&gt;)(other: https://developer.android.com/reference/java/util/Comparator.html<T!>!)` `https://developer.android.com/reference/java/util/Comparator.html<T!>!` `https://developer.android.com/reference/java/util/Comparator.html#thenComparingDouble(java.util.function.ToDoubleFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToDoubleFunction.html<T!>!)` `https://developer.android.com/reference/java/util/Comparator.html<T!>!` `https://developer.android.com/reference/java/util/Comparator.html#thenComparingInt(java.util.function.ToIntFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToIntFunction.html<T!>!)` `https://developer.android.com/reference/java/util/Comparator.html<T!>!` `https://developer.android.com/reference/java/util/Comparator.html#thenComparingLong(java.util.function.ToLongFunction&lt;? super T&gt;)(keyExtractor: https://developer.android.com/reference/java/util/function/ToLongFunction.html<T!>!)` From [androidx.recyclerview.widget.ListUpdateCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback) `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback#onInserted(int,int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, count: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Called when `count` number of items are inserted at the given position.
`abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)(fromPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, toPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Called when an item changes its position in the list.
`abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, count: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Called when `count` number of items are removed from the given position.

## 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(oldItem: T2!, newItem: T2!): Boolean
```

Called by the SortedList when it wants to check whether two items have the same data or not. SortedList uses this information to decide whether it should call `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)` or not.

SortedList uses this method to check equality instead of `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 SortedList with a `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter`, you should return whether the items' visual representations are the same or not.

| Parameters |
|---|---|
| `oldItem: T2!` | The previous representation of the object. |
| `newItem: T2!` | The new object that replaces the previous one. |

| Returns |
|---|---|
| `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(item1: T2!, item2: T2!): Boolean
```

Called by the SortedList to decide whether two objects represent the same Item or not.

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

| Parameters |
|---|---|
| `item1: T2!` | The first item to check. |
| `item2: T2!` | The second item to check. |

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

### compare

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

```
abstract fun compare(o1: T2!, o2: T2!): Int
```

Similar to `https://developer.android.com/reference/java/util/Comparator.html#compare(T, T)`, should compare two and return how they should be ordered.

| Parameters |
|---|---|
| `o1: T2!` | The first object to compare. |
| `o2: T2!` | The second object to compare. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. |

### getChangePayload

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

```
fun getChangePayload(item1: T2!, item2: T2!): Any?
```

When `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)` returns `true` for two items and `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` returns false for them, `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback` calls this method to get a payload about the change.

For example, if you are using `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback` with `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView`, you can return the particular field that changed in the item and your `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 |
|---|---|
| `item1: T2!` | The first item to check. |
| `item2: T2!` | The second item to check. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | A payload object that represents the changes between the two items. |

### onChanged

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

```
abstract fun onChanged(position: Int, count: Int): Unit
```

Called by the SortedList when the item at the given position is updated.

| Parameters |
|---|---|
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The position of the item which has been updated. |
| `count: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The number of items which has changed. |

### onChanged

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

```
fun onChanged(position: Int, count: Int, payload: Any!): Unit
```

Called when count} number of items are updated at the given position.