-
public class 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 compare 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public abstract class
SortedList.Callback
The class that controls the behavior of the 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 underlyingdata.
public class
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'twant 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 callconvert individual
onInserted(index, 1)
calls into oneonInserted(index, N)
if items are added into consecutive indices. This changecan help RecyclerView resolve changes much more easily.If consecutive changes in the SortedList are not suitable for batching, BatchingCallbackdispatches them as soon as such case is detected. After your edits on the SortedList iscomplete, you must always call dispatchLastEvent to flushall changes to the Callback.
-
Field Summary
Fields Modifier and Type Field Description public final static int
INVALID_POSITION
-
Constructor Summary
Constructors Constructor Description SortedList(Class<T> klass, SortedList.Callback<T> callback)
Creates a new SortedList of type T. SortedList(Class<T> klass, SortedList.Callback<T> callback, int initialCapacity)
Creates a new SortedList of type T.
-
Method Summary
Modifier and Type Method Description int
size()
The number of items in the list. int
add(T item)
Adds the given item to the list. void
addAll(@NonNull() Array<T> items, boolean mayModifyInput)
Adds the given items to the list. void
addAll(@NonNull() Array<T> items)
Adds the given items to the list. void
addAll(@NonNull() Collection<T> items)
Adds the given items to the list. void
replaceAll(@NonNull() Array<T> items, boolean mayModifyInput)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate. void
replaceAll(@NonNull() Array<T> items)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate. void
replaceAll(@NonNull() Collection<T> items)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate. void
beginBatchedUpdates()
Batches adapter updates that happen after calling this method and before calling endBatchedUpdates. void
endBatchedUpdates()
Ends the update transaction and dispatches any remaining event to the callback. boolean
remove(T item)
Removes the provided item from the list and calls onRemoved. T
removeItemAt(int index)
Removes the item at the given index and calls onRemoved. void
updateItemAt(int index, T item)
Updates the item at the given index and calls onChanged and/or onMoved if necessary. void
recalculatePositionOfItemAt(int index)
This method can be used to recalculate the position of the item at the given index, withouttriggering an onChanged callback. T
get(int index)
Returns the item at the given index. int
indexOf(T item)
Returns the position of the provided item. void
clear()
Removes all items from the SortedList. -
-
Constructor Detail
-
SortedList
SortedList(Class<T> klass, SortedList.Callback<T> callback)
Creates a new SortedList of type T.- Parameters:
klass
- The class of the contents of the SortedList.callback
- The callback that controls the behavior of SortedList.
-
SortedList
SortedList(Class<T> klass, SortedList.Callback<T> callback, int initialCapacity)
Creates a new SortedList of type T.- Parameters:
klass
- The class of the contents of the SortedList.callback
- The callback that controls the behavior of SortedList.initialCapacity
- The initial capacity to hold items.
-
-
Method Detail
-
size
int size()
The number of items in the list.
-
add
int add(T item)
Adds the given item to the list. If this is a new item, SortedList calls onInserted.
If the item already exists in the list and its sorting criteria is not changed, it isreplaced with the existing Item. SortedList uses areItemsTheSame to check if two items are the same itemand uses areContentsTheSame to decide whether it shouldcall onChanged or not. In both cases, it always removes thereference to the old item and puts the new item into the backing array even if areContentsTheSame returns false.
If the sorting criteria of the item is changed, SortedList won't be able to findits duplicate in the list which will result in having a duplicate of the Item in the list.If you need to update sorting criteria of an item that already exists in the list,use updateItemAt. You can find the index of the item using indexOf before you update the object.
- Parameters:
item
- The item to be added into the list.
-
addAll
void addAll(@NonNull() Array<T> items, boolean mayModifyInput)
Adds the given items to the list. Equivalent to calling add in a loop,except the callback events may be in a different order/granularity since addAll can batchthem for better performance.
If allowed, will reference the input array during, and possibly after, the operation to avoidextra memory allocation, in which case you should not continue to reference or modify thearray yourself.
- Parameters:
items
- Array of items to be added into the list.mayModifyInput
- If true, SortedList is allowed to modify and permanently reference theinput array.
-
addAll
void addAll(@NonNull() Array<T> items)
Adds the given items to the list. Does not modify or retain the input.
- Parameters:
items
- Array of items to be added into the list.
-
addAll
void addAll(@NonNull() Collection<T> items)
Adds the given items to the list. Does not modify or retain the input.
- Parameters:
items
- Collection of items to be added into the list.
-
replaceAll
void replaceAll(@NonNull() Array<T> items, boolean mayModifyInput)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate.
If allowed, will reference the input array during, and possibly after, the operation to avoidextra memory allocation, in which case you should not continue to reference or modify thearray yourself.
Note: this method does not detect moves or dispatch onMoved events. It instead treats moves as a removefollowed by an add and therefore dispatches onRemoved and onRemoved events. See DiffUtil if you wantyour implementation to dispatch move events.
- Parameters:
items
- Array of items to replace current items.mayModifyInput
- If true, SortedList is allowed to modify and permanently reference theinput array.
-
replaceAll
void replaceAll(@NonNull() Array<T> items)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate. Does not modify or retain the input.
- Parameters:
items
- Array of items to replace current items.
-
replaceAll
void replaceAll(@NonNull() Collection<T> items)
Replaces the current items with the new items, dispatching ListUpdateCallback eventsfor each change detected as appropriate. Does not modify or retain the input.
- Parameters:
items
- Array of items to replace current items.
-
beginBatchedUpdates
void beginBatchedUpdates()
Batches adapter updates that happen after calling this method and before calling endBatchedUpdates. For example, if you add multiple items in a loopand they are placed into consecutive indices, SortedList calls onInserted only once with the proper item count. If an eventcannot be merged with the previous event, the previous event is dispatchedto the callback instantly.
After running your data updates, you must call endBatchedUpdates which will dispatch any deferred data change event to the current callback.
A sample implementation may look like this:
mSortedList.beginBatchedUpdates(); try { mSortedList.add(item1) mSortedList.add(item2) mSortedList.remove(item3) ... } finally { mSortedList.endBatchedUpdates(); }
Instead of using this method to batch calls, you can use a Callback that extends BatchedCallback. In that case, you must make sure that you are manually calling dispatchLastEvent right after you complete your data changes.Failing to do so may create data inconsistencies with the Callback.
If the current Callback is an instance of BatchedCallback, calling this methodhas no effect.
-
endBatchedUpdates
void endBatchedUpdates()
Ends the update transaction and dispatches any remaining event to the callback.
-
remove
boolean remove(T item)
Removes the provided item from the list and calls onRemoved.
- Parameters:
item
- The item to be removed from the list.
-
removeItemAt
T removeItemAt(int index)
Removes the item at the given index and calls onRemoved.
- Parameters:
index
- The index of the item to be removed.
-
updateItemAt
void updateItemAt(int index, T item)
Updates the item at the given index and calls onChanged and/or onMoved if necessary.
You can use this method if you need to change an existing Item such that its position in thelist may change.
If the new object is a different object (
get(index) != item
) and areContentsTheSame returnstrue
, SortedListavoids calling onChanged otherwise it calls onChanged.If the new position of the item is different than the provided
index
,SortedListcalls onMoved.- Parameters:
index
- The index of the item to replaceitem
- The item to replace the item at the given Index.
-
recalculatePositionOfItemAt
void recalculatePositionOfItemAt(int index)
This method can be used to recalculate the position of the item at the given index, withouttriggering an onChanged callback.
If you are editing objects in the list such that their position in the list may change butyou don't want to trigger an onChange animation, you can use this method to re-position it.If the item changes position, SortedList will call onMoved withoutcalling onChanged.
A sample usage may look like:
In the example above, because the sorting criteria of the item has been changed,mSortedList.indexOf(item) will not be able to find the item. This is why the code abovefirstgets the position before editing the item, edits it and informs the SortedList that itemshould be repositioned.final int position = mSortedList.indexOf(item); item.incrementPriority(); // assume items are sorted by priority mSortedList.recalculatePositionOfItemAt(position);
- Parameters:
index
- The current index of the Item whose position should be re-calculated.
-
get
T get(int index)
Returns the item at the given index.
- Parameters:
index
- The index of the item to retrieve.
-
indexOf
int indexOf(T item)
Returns the position of the provided item.
- Parameters:
item
- The item to query for position.
-
clear
void clear()
Removes all items from the SortedList.
-
-
-
-