# SortedList.BatchedCallback

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.BatchedCallback) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
class SortedList.BatchedCallback<T2> : SortedList.Callback<T2!>
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.recyclerview.widget.SortedList.Callback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback)\<T2!\> ||
|   | ↳ | [androidx.recyclerview.widget.SortedList.BatchedCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback) |

*** ** * ** ***

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 `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#dispatchLastEvent()` to flush all changes to the Callback.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#BatchedCallback(androidx.recyclerview.widget.SortedList.Callback<T2>)(wrappedCallback: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback<T2!>!)` Creates a new BatchedCallback that wraps the provided Callback. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#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. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#areItemsTheSame(T2,T2)(item1: T2!, item2: T2!)` Called by the SortedList to decide whether two objects represent the same Item or not. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#compare(T2,T2)(o1: T2!, o2: T2!)` Similar to java.util.Comparator#compare(Object, Object), should compare two and return how they should be ordered. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#dispatchLastEvent()()` This method dispatches any pending event notifications to the wrapped Callback. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#getChangePayload(T2,T2)(item1: T2!, item2: T2!)` When #areItemsTheSame(T2, T2) returns true} for two items and #areContentsTheSame(T2, T2) returns false for them, calls this method to get a payload about the change. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#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.BatchedCallback#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!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#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. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#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. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#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. |

### Inherited functions

From [java.util.Comparator](https://developer.android.com/reference/java/util/Comparator.html) `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/java/util/Comparator.html#compare(T, T)(p: T!, p1: T!)` `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#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.

## Public constructors

### BatchedCallback

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

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

Creates a new BatchedCallback that wraps the provided Callback.

| Parameters |
|---|---|
| `wrappedCallback: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.Callback<T2!>!` | The Callback which should received the data change callbacks. Other method calls (e.g. `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SortedList.BatchedCallback#compare(T2,T2)` from the SortedList are directly forwarded to this Callback. |

## Public functions

### areContentsTheSame

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

```
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 #onChanged(int, int) or not.

SortedList uses this method to check equality instead of Object#equals(Object) so that you can change its behavior depending on your UI.

For example, if you are using SortedList with a RecyclerView.Adapter, you should return whether the items' visual representations are the same or not.

### areItemsTheSame

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

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

### compare

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

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

Similar to java.util.Comparator#compare(Object, Object), should compare two and return how they should be ordered.

### dispatchLastEvent

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

```
fun dispatchLastEvent(): Unit
```

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.

### getChangePayload

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

When #areItemsTheSame(T2, T2) returns true} for two items and #areContentsTheSame(T2, T2) returns false for them, calls this method to get a payload about the change.

For example, if you are using with , you can return the particular field that changed in the item and your ItemAnimator can use that information to run the correct animation.

Default implementation returns null}.

### onChanged

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

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

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

### onChanged

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

### onInserted

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

```
fun onInserted(position: Int, count: Int): Unit
```

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

### onMoved

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

```
fun onMoved(fromPosition: Int, toPosition: Int): Unit
```

Called when an item changes its position in the list.

### onRemoved

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

```
fun onRemoved(position: Int, count: Int): Unit
```

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