# SortedList.Callback

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1  
Summary:[Ctors](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#)  

# SortedList.Callback

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public
static

abstract
class
SortedList.Callback
`  
`

extends Object


``


implements

Comparator<T2>,

`[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)`


`

|---|---------------------------------------------------|
| java.lang.Object                                     ||
| ↳ | android.support.v7.util.SortedList.Callback\<T2\> |

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#)Known Direct Subclasses [SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback)\<T2\>,[SortedListAdapterCallback](https://developer.android.com/reference/android/support/v7/widget/util/SortedListAdapterCallback)\<T2\> |-------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback)\<T2\>      | A callback implementation that can batch notify events dispatched by the SortedList.                                                                                                                                                                                                                                                                 | | [SortedListAdapterCallback](https://developer.android.com/reference/android/support/v7/widget/util/SortedListAdapterCallback)\<T2\> | A[SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)implementation that can bind a[SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList)to a[RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter). | |

<br />

*** ** * ** ***

The class that controls the behavior of the[SortedList](https://developer.android.com/reference/android/support/v7/util/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                                                           ||
|------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#SortedList.Callback())`() ` |

|                                                                                                                                                                                                                                                                                                                                    ### Public methods                                                                                                                                                                                                                                                                                                                                     ||
|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract boolean` | ` `[areContentsTheSame](https://developer.android.com/reference/android/support/v7/util/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` | ` `[areItemsTheSame](https://developer.android.com/reference/android/support/v7/util/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`     | ` `[compare](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#compare(T2, T2))`(T2 o1, T2 o2) ` Similar to[compare(Object, Object)](https://developer.android.com/reference/java/util/Comparator#compare(T, T)), should compare two and return how they should be ordered.                                                                                                                                                                                                                                                                                                                                                        |
| ` Object`           | ` `[getChangePayload](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#getChangePayload(T2, T2))`(T2 item1, T2 item2) ` When[areItemsTheSame(T2, T2)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areItemsTheSame(T2, T2))returns`true`for two items and[areContentsTheSame(T2, T2)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#areContentsTheSame(T2, T2))returns false for them,[SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)calls this method to get a payload about the change. |
| ` void`             | ` `[onChanged](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int, java.lang.Object))`(int position, int count, Object payload) ` Called when`count`number of items are updated at the given position.                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` abstract void`    | ` `[onChanged](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int))`(int position, int count) ` Called by the SortedList when the item at the given position is updated.                                                                                                                                                                                                                                                                                                                                                                                                                                         |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#)From interface` java.util.Comparator ` |---------------------------------------------------------------|--------------------------------------------------------------------------------------| | ` abstract int`                                               | ` compare(T2 arg0, T2 arg1) `                                                        | | ` static <T, U> Comparator<T2>`                               | ` comparing(Function<? super T, ? extends U> arg0, Comparator<? super U> arg1) `     | | ` static <T, U extends Comparable<? super U>> Comparator<T2>` | ` comparing(Function<? super T, ? extends U> arg0) `                                 | | ` static <T> Comparator<T2>`                                  | ` comparingDouble(ToDoubleFunction<? super T> arg0) `                                | | ` static <T> Comparator<T2>`                                  | ` comparingInt(ToIntFunction<? super T> arg0) `                                      | | ` static <T> Comparator<T2>`                                  | ` comparingLong(ToLongFunction<? super T> arg0) `                                    | | ` abstract boolean`                                           | ` equals(Object arg0) `                                                              | | ` static <T extends Comparable<? super T>> Comparator<T>`     | ` naturalOrder() `                                                                   | | ` static <T> Comparator<T2>`                                  | ` nullsFirst(Comparator<? super T> arg0) `                                           | | ` static <T> Comparator<T2>`                                  | ` nullsLast(Comparator<? super T> arg0) `                                            | | ` static <T extends Comparable<? super T>> Comparator<T>`     | ` reverseOrder() `                                                                   | | ` default Comparator<T2>`                                     | ` reversed() `                                                                       | | ` default <U extends Comparable<? super U>> Comparator<T2>`   | ` thenComparing(Function<? super T, ? extends U> arg0) `                             | | ` default <U> Comparator<T2>`                                 | ` thenComparing(Function<? super T, ? extends U> arg0, Comparator<? super U> arg1) ` | | ` default Comparator<T2>`                                     | ` thenComparing(Comparator<? super T> arg0) `                                        | | ` default Comparator<T2>`                                     | ` thenComparingDouble(ToDoubleFunction<? super T> arg0) `                            | | ` default Comparator<T2>`                                     | ` thenComparingInt(ToIntFunction<? super T> arg0) `                                  | | ` default Comparator<T2>`                                     | ` thenComparingLong(ToLongFunction<? super T> arg0) `                                | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#)From interface` `[android.support.v7.util.ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)` ` |------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[onChanged](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onChanged(int, int, java.lang.Object))`(int position, int count, Object payload) ` Called when`count`number of items are updated at the given position. | | ` abstract void` | ` `[onInserted](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onInserted(int, int))`(int position, int count) ` Called when`count`number of items are inserted at the given position.                                | | ` abstract void` | ` `[onMoved](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onMoved(int, int))`(int fromPosition, int toPosition) ` Called when an item changes its position in the list.                                             | | ` abstract void` | ` `[onRemoved](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback#onRemoved(int, int))`(int position, int count) ` Called when`count`number of items are removed from the given position.                                 | ||

## Public constructors

### SortedList.Callback

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
SortedList.Callback ()
```

<br />

<br />

## Public methods

### areContentsTheSame

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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[onChanged(int, int)](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback#onChanged(int, int))or not.

SortedList uses this method to check equality instead of[equals(Object)](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))so that you can change its behavior depending on your UI.

For example, if you are using SortedList with a[RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter), you should return whether the items' visual representations are the same or not.

<br />

|                               Parameters                               ||
|-----------|-------------------------------------------------------------|
| `oldItem` | `T2`: The previous representation of the object. <br />     |
| `newItem` | `T2`: The new object that replaces the previous one. <br /> |

|                                             Returns                                              ||
|-----------|---------------------------------------------------------------------------------------|
| `boolean` | True if the contents of the items are the same or false if they are different. <br /> |

### areItemsTheSame

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `item1` | `T2`: The first item to check. <br />  |
| `item2` | `T2`: The second item to check. <br /> |

|                                              Returns                                              ||
|-----------|----------------------------------------------------------------------------------------|
| `boolean` | True if the two items represent the same object or false if they are different. <br /> |

### compare

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

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

<br />

|                    Parameters                    ||
|------|--------------------------------------------|
| `o1` | `T2`: The first object to compare. <br />  |
| `o2` | `T2`: The second object to compare. <br /> |

|                                                                 Returns                                                                 ||
|-------|----------------------------------------------------------------------------------------------------------------------------------|
| `int` | a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. <br /> |

### getChangePayload

added in[version 27.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
Object getChangePayload (T2 item1, 
                T2 item2)
```

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

For example, if you are using[SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)with[RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView), you can return the particular field that changed in the item and your[ItemAnimator](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemAnimator)can use that information to run the correct animation.

Default implementation returns`null`.

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `item1` | `T2`: The first item to check. <br />  |
| `item2` | `T2`: The second item to check. <br /> |

|                                       Returns                                        ||
|----------|----------------------------------------------------------------------------|
| `Object` | A payload object that represents the changes between the two items. <br /> |

### onChanged

added in[version 25.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

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

<br />

|                                 Parameters                                 ||
|------------|----------------------------------------------------------------|
| `position` | `int`: The position of the item which has been updated. <br /> |
| `count`    | `int`: The number of items which has changed. <br />           |
| `payload`  | `Object` <br />                                                |

### onChanged

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

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

<br />

|                                 Parameters                                 ||
|------------|----------------------------------------------------------------|
| `position` | `int`: The position of the item which has been updated. <br /> |
| `count`    | `int`: The number of items which has changed. <br />           |

-

  ## Interfaces

  - [ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)
-

  ## Classes

  - [AdapterListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/AdapterListUpdateCallback)
  - [AsyncListUtil](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil)
  - [AsyncListUtil.DataCallback](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil.DataCallback)
  - [AsyncListUtil.ViewCallback](https://developer.android.com/reference/android/support/v7/util/AsyncListUtil.ViewCallback)
  - [BatchingListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/BatchingListUpdateCallback)
  - [DiffUtil](https://developer.android.com/reference/android/support/v7/util/DiffUtil)
  - [DiffUtil.Callback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.Callback)
  - [DiffUtil.DiffResult](https://developer.android.com/reference/android/support/v7/util/DiffUtil.DiffResult)
  - [DiffUtil.ItemCallback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.ItemCallback)
  - [SortedList](https://developer.android.com/reference/android/support/v7/util/SortedList)
  - [SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback)
  - [SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)