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

*** ** * ** ***


```
public abstract class SortedList.Callback<T2> implements Comparator<T2>, ListUpdateCallback
```

<br />

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

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

*** ** * ** ***

The class that controls the behavior of the `https://developer.android.com/reference/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/androidx/recyclerview/widget/SortedList.Callback#Callback()()` |

| ### Public methods |
|---|---|
| `abstract boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)(T2 oldItem, T2 newItem)` Called by the SortedList when it wants to check whether two items have the same data or not. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)(T2 item1, T2 item2)` Called by the SortedList to decide whether two objects represent the same Item or not. |
| `abstract int` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#compare(T2,T2)(T2 o1, T2 o2)` 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://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#getChangePayload(T2,T2)(T2 item1, T2 item2)` When `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areItemsTheSame(T2,T2)` returns `true` for two items and `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#areContentsTheSame(T2,T2)` returns false for them, `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback` calls this method to get a payload about the change. |
| `abstract void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int)(int position, int count)` Called by the SortedList when the item at the given position is updated. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SortedList.Callback#onChanged(int,int,java.lang.Object)(int position, int count, https://developer.android.com/reference/java/lang/Object.html payload)` Called when count} number of items are updated at the given position. |

### Inherited methods

From [java.util.Comparator](https://developer.android.com/reference/java/util/Comparator.html) `static https://developer.android.com/reference/java/util/Comparator.html<T>` `<T, U extends https://developer.android.com/reference/java/lang/Comparable.html<U>> https://developer.android.com/reference/java/util/Comparator.html#comparing(java.util.function.Function&lt;? super T,? extends U&gt;)(https://developer.android.com/reference/java/util/function/Function.html<T, U> keyExtractor)` `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;)(https://developer.android.com/reference/java/util/function/ToDoubleFunction.html<T> keyExtractor)` `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;)(https://developer.android.com/reference/java/util/function/ToIntFunction.html<T> keyExtractor)` `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;)(https://developer.android.com/reference/java/util/function/ToLongFunction.html<T> keyExtractor)` `static https://developer.android.com/reference/java/util/Comparator.html<T>` `<T extends https://developer.android.com/reference/java/lang/Comparable.html<T>> https://developer.android.com/reference/java/util/Comparator.html#naturalOrder()()` `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;)(https://developer.android.com/reference/java/util/Comparator.html<T> comparator)` `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;)(https://developer.android.com/reference/java/util/Comparator.html<T> comparator)` `static https://developer.android.com/reference/java/util/Comparator.html<T>` `<T extends https://developer.android.com/reference/java/lang/Comparable.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;)(https://developer.android.com/reference/java/util/Comparator.html<T> other)` `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;)(https://developer.android.com/reference/java/util/function/ToDoubleFunction.html<T> keyExtractor)` `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;)(https://developer.android.com/reference/java/util/function/ToIntFunction.html<T> keyExtractor)` `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;)(https://developer.android.com/reference/java/util/function/ToLongFunction.html<T> keyExtractor)` From [androidx.recyclerview.widget.ListUpdateCallback](https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback) `abstract void` `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onInserted(int,int)(int position, int count)`

Called when `count` number of items are inserted at the given position.
`abstract void` `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onMoved(int,int)(int fromPosition, int toPosition)`

Called when an item changes its position in the list.
`abstract void` `https://developer.android.com/reference/androidx/recyclerview/widget/ListUpdateCallback#onRemoved(int,int)(int position, int count)`

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

## Public constructors

### Callback

Added in 1.0.0

```
public Callback()
```

## Public methods

### areContentsTheSame

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

```
public abstract boolean areContentsTheSame(T2 oldItem, T2 newItem)
```

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/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/androidx/recyclerview/widget/RecyclerView.Adapter`, you should return whether the items' visual representations are the same or not.

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

| Returns |
|---|---|
| `boolean` | 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)

```
public abstract boolean areItemsTheSame(T2 item1, T2 item2)
```

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 |
|---|---|
| `T2 item1` | The first item to check. |
| `T2 item2` | The second item to check. |

| Returns |
|---|---|
| `boolean` | 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)

```
public abstract int compare(T2 o1, T2 o2)
```

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 |
|---|---|
| `T2 o1` | The first object to compare. |
| `T2 o2` | The second object to compare. |

| Returns |
|---|---|
| `int` | 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)

```
public @Nullable Object getChangePayload(T2 item1, T2 item2)
```

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

Default implementation returns `null`.

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.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)

```
public abstract void onChanged(int position, int count)
```

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

| Parameters |
|---|---|
| `int position` | The position of the item which has been updated. |
| `int count` | The number of items which has changed. |

### onChanged

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

```
public void onChanged(int position, int count, Object payload)
```

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