SortedList

public class SortedList
extends Object

java.lang.Object
   ↳ androidx.recyclerview.widget.SortedList<T>


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 SortedList.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 SortedList.Callback parameter.

Summary

Nested classes

class SortedList.BatchedCallback<T2>

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

class SortedList.Callback<T2>

The class that controls the behavior of the SortedList

Constants

int INVALID_POSITION

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

Public constructors

SortedList(Class<T> klass, Callback<T> callback)

Creates a new SortedList of type T.

SortedList(Class<T> klass, Callback<T> callback, int initialCapacity)

Creates a new SortedList of type T.

Public methods

int add(T item)

Adds the given item to the list.

void addAll(T... items)

Adds the given items to the list.

void addAll(Collection<T> items)

Adds the given items to the list.

void addAll(T[] items, boolean mayModifyInput)

Adds the given items to the list.

void beginBatchedUpdates()

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

void clear()

Removes all items from the SortedList.

void endBatchedUpdates()

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

T get(int index)

Returns the item at the given index.

int indexOf(T item)

Returns the position of the provided item.

void recalculatePositionOfItemAt(int index)

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

boolean remove(T item)

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

T removeItemAt(int index)

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

void replaceAll(T... items)

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

void replaceAll(Collection<T> items)

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

void replaceAll(T[] items, boolean mayModifyInput)

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

int size()

The number of items in the list.

void updateItemAt(int index, T item)

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

Inherited methods

Constants

INVALID_POSITION

public static final int INVALID_POSITION

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

Constant Value: -1 (0xffffffff)

Public constructors

SortedList

public SortedList (Class<T> klass, 
                Callback<T> callback)

Creates a new SortedList of type T.

Parameters
klass Class: The class of the contents of the SortedList.

callback Callback: The callback that controls the behavior of SortedList.