Android 12 Developer Preview is here! Try it out, and give us your feedback!

SortedList

open class SortedList<T : Any!>
kotlin.Any
   ↳ androidx.recyclerview.widget.SortedList

A Sorted list implementation that can keep items in order and also notify for changes in the list such that it can be bound to a RecyclerView.Adapter.

It keeps items ordered using the Callback#compare(Object, Object) method and uses binary search to retrieve items. If the sorting criteria of your items may change, make sure you call appropriate methods while editing them to avoid data inconsistencies.

You can control the order of items and change notifications via the Callback parameter.

Summary

Nested classes
open

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

abstract

The class that controls the behavior of the SortedList.

Constants
static Int

Used by indexOf(Object) when the item cannot be found in the list.

Public constructors
<init>(@NonNull klass: Class<T>, @NonNull callback: SortedList.Callback<T>)

Creates a new SortedList of type T.

<init>(@NonNull klass: Class<T>, @NonNull callback: SortedList.Callback<T>, initialCapacity: Int)

Creates a new SortedList of type T.

Public methods
open Int
add(item: T)

Adds the given item to the list.

open Unit
addAll(@NonNull items: Array<T>, mayModifyInput: Boolean)

Adds the given items to the list.

open Unit
addAll(@NonNull vararg items: T)

Adds the given items to the list.

open Unit
addAll(@NonNull items: MutableCollection<T>)

Adds the given items to the list.

open Unit

Batches adapter updates that happen after calling this method and before calling endBatchedUpdates().

open Unit

Removes all items from the SortedList.

open Unit

Ends the update transaction and dispatches any remaining event to the callback.

open T
get(index: Int)

Returns the item at the given index.

open Int
indexOf(item: T)

Returns the position of the provided item.

open Unit

This method can be used to recalculate the position of the item at the given index, without triggering an Callback#onChanged(int, int) callback.

open Boolean
remove(item: T)

Removes the provided item from the list and calls Callback#onRemoved(int, int).

open T
removeItemAt(index: Int)

Removes the item at the given index and calls Callback#onRemoved(int, int).

open Unit
replaceAll(@NonNull items: Array<T>, mayModifyInput: Boolean)

Replaces the current items with the new items, dispatching ListUpdateCallback events for each change detected as appropriate.

open Unit
replaceAll(@NonNull vararg items: T)

Replaces the current items with the new items, dispatching ListUpdateCallback events for each change detected as appropriate.

open Unit
replaceAll(@NonNull items: MutableCollection<T>)

Replaces the current items with the new items, dispatching ListUpdateCallback events for each change detected as appropriate.

open Int

The number of items in the list.

open Unit
updateItemAt(index: Int, item: T)

Updates the item at the given index and calls Callback#onChanged(int, int) and/or Callback#onMoved(int, int) if necessary.

Constants

INVALID_POSITION

static val INVALID_POSITION: Int

Used by indexOf(Object) when the item cannot be found in the list.

Value: -1

Public constructors

<init>

SortedList(
    @NonNull klass: Class<T>