# SortedList.BatchedCallback

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.BatchedCallback#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#)  

# SortedList.BatchedCallback

| 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


class
SortedList.BatchedCallback
`  
`


``

extends `[Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)`<T2>


``


`

|---|---|----------------------------------------------------------|
| java.lang.Object                                               |||
| ↳ | [android.support.v7.util.SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)\<T2\> ||
|   | ↳ | android.support.v7.util.SortedList.BatchedCallback\<T2\> |

<br />

*** ** * ** ***

A callback implementation that can batch notify events dispatched by the SortedList.

This class can be useful if you want to do multiple operations on a SortedList but don't want to dispatch each event one by one, which may result in a performance issue.

For example, if you are going to add multiple items to a SortedList, BatchedCallback call convert individual`onInserted(index, 1)`calls into one`onInserted(index, N)`if items are added into consecutive indices. This change can help RecyclerView resolve changes much more easily.

If consecutive changes in the SortedList are not suitable for batching, BatchingCallback dispatches them as soon as such case is detected. After your edits on the SortedList is complete, you**must** always call[dispatchLastEvent()](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#dispatchLastEvent())to flush all changes to the Callback.

## Summary

|                                                                                                                                                                                       ### Public constructors                                                                                                                                                                                        ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SortedList.BatchedCallback](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#SortedList.BatchedCallback(android.support.v7.util.SortedList.Callback<T2>))`(`[Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)`<T2> wrappedCallback) ` Creates a new BatchedCallback that wraps the provided Callback. |

|                                                                                                                                                                                                                                                                                                                                   ### Public methods                                                                                                                                                                                                                                                                                                                                    ||
|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean` | ` `[areContentsTheSame](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#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.                                                                                                                                                                                                                                                                                                                                                                                                      |
| ` boolean` | ` `[areItemsTheSame](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#areItemsTheSame(T2, T2))`(T2 item1, T2 item2) ` Called by the SortedList to decide whether two objects represent the same Item or not.                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ` int`     | ` `[compare](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#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.                                                                                                                                                                                                                                                                                                                                                        |
| ` void`    | ` `[dispatchLastEvent](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#dispatchLastEvent())`() ` This method dispatches any pending event notifications to the wrapped Callback.                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` Object`  | ` `[getChangePayload](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#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.BatchedCallback#onChanged(int, int, java.lang.Object))`(int position, int count, Object payload) ` Called when`count`number of items are updated at the given position.                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` void`    | ` `[onChanged](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#onChanged(int, int))`(int position, int count) ` Called by the SortedList when the item at the given position is updated.                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ` void`    | ` `[onInserted](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#onInserted(int, int))`(int position, int count) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ` void`    | ` `[onMoved](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#onMoved(int, int))`(int fromPosition, int toPosition) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ` void`    | ` `[onRemoved](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#onRemoved(int, int))`(int position, int count) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#)From class` `[android.support.v7.util.SortedList.Callback](https://developer.android.com/reference/android/support/v7/util/SortedList.Callback)` ` |---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` 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.                                                                                                                                                                                                                                                                                                                                                                                                                                         | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#)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.BatchedCallback#)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.BatchedCallback#)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.BatchedCallback

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

```
SortedList.BatchedCallback (Callback<T2> wrappedCallback)
```

Creates a new BatchedCallback that wraps the provided Callback.

<br />

|                                                                                                                                                          Parameters                                                                                                                                                           ||
|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `wrappedCallback` | `Callback`: The Callback which should received the data change callbacks. Other method calls (e.g.[compare(Object, Object)](https://developer.android.com/reference/android/support/v7/util/SortedList.BatchedCallback#compare(T2, T2))from the SortedList are directly forwarded to this Callback. <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 /> |

### dispatchLastEvent

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

```
void dispatchLastEvent ()
```

This method dispatches any pending event notifications to the wrapped Callback. You**must**always call this method after you are done with editing the SortedList.

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

### onInserted

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

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

<br />

<br />

|        Parameters        ||
|------------|--------------|
| `position` | `int` <br /> |
| `count`    | `int` <br /> |

### onMoved

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

```
void onMoved (int fromPosition, 
                int toPosition)
```

<br />

<br />

|          Parameters          ||
|----------------|--------------|
| `fromPosition` | `int` <br /> |
| `toPosition`   | `int` <br /> |

### onRemoved

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

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

<br />

<br />

|        Parameters        ||
|------------|--------------|
| `position` | `int` <br /> |
| `count`    | `int` <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)