SortedList.BatchedCallback


public class SortedList.BatchedCallback<T2> extends SortedList.Callback


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 to flush all changes to the Callback.

Summary

Public constructors

BatchedCallback(SortedList.Callback<T2> wrappedCallback)

Creates a new BatchedCallback that wraps the provided Callback.

Public methods

boolean
areContentsTheSame(T2 oldItem, T2 newItem)

Called by the SortedList when it wants to check whether two items have the same data or not.

boolean
areItemsTheSame(T2 item1, T2 item2)

Called by the SortedList to decide whether two objects represent the same Item or not.

int
compare(T2 o1, T2 o2)

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

void

This method dispatches any pending event notifications to the wrapped Callback.

@Nullable Object
getChangePayload(T2 item1, T2 item2)

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.

void
onChanged(int position, int count)

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

void
onChanged(int position, int count, Object payload)
void
onInserted(int position, int count)

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

void
onMoved(int fromPosition, int toPosition)

Called when an item changes its position in the list.

void
onRemoved(int position, int count)

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

Inherited methods

From java.util.Comparator
abstract int
compare(T p, T p1)
static Comparator<T>
<T, U> comparing(
    Function<T, U> keyExtractor,
    Comparator<U> keyComparator
)
static Comparator<T>
<T> comparingDouble(ToDoubleFunction<T> keyExtractor)
static Comparator<T>
<T> comparingInt(ToIntFunction<T> keyExtractor)
static Comparator<T>
<T> comparingLong(ToLongFunction<T> keyExtractor)
static Comparator<T>
<T extends Comparable<T>> naturalOrder()
static Comparator<T>
<T> nullsFirst(Comparator<T> comparator)
static Comparator<T>
<T> nullsLast(Comparator<T> comparator)
static Comparator<T>
<T extends Comparable<T>> reverseOrder()
Comparator<T>
Comparator<T>
Comparator<T>
Comparator<T>
Comparator<T>
From androidx.recyclerview.widget.ListUpdateCallback
abstract void
onChanged(int position, int count, @Nullable Object payload)

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

Public constructors

BatchedCallback

Added in 1.0.0
public BatchedCallback(SortedList.Callback<T2> wrappedCallback)

Creates a new BatchedCallback that wraps the provided Callback.

Parameters
SortedList.Callback<T2> wrappedCallback

The Callback which should received the data change callbacks. Other method calls (e.g. compare from the SortedList are directly forwarded to this Callback.

Public methods

areContentsTheSame

Added in 1.4.0-rc01
public 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) 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-rc01
public 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.

compare

Added in 1.4.0-rc01
public int compare(T2 o1, T2 o2)

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

dispatchLastEvent

Added in 1.0.0
public 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.

getChangePayload

public @Nullable Object getChangePayload(T2 item1, T2 item2)

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-rc01
public void onChanged(int position, int count)

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

onChanged

public void onChanged(int position, int count, Object payload)

onInserted

Added in 1.4.0-rc01
public void onInserted(int position, int count)

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

onMoved

Added in 1.4.0-rc01
public void onMoved(int fromPosition, int toPosition)

Called when an item changes its position in the list.

onRemoved

Added in 1.4.0-rc01
public void onRemoved(int position, int count)

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