public static class SortedList.BatchedCallback<T2> extends SortedList.Callback<T2>
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.
Constructor and Description |
---|
BatchedCallback(SortedList.Callback<T2> wrappedCallback)
Creates a new BatchedCallback that wraps the provided Callback.
|
Modifier and Type | Method and Description |
---|---|
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 object represent the same Item or not.
|
int |
compare(T2 o1,
T2 o2)
Similar to
Comparator.compare(Object, Object) , should compare two and
return how they should be ordered. |
void |
dispatchLastEvent()
This method dispatches any pending event notifications to the wrapped Callback.
|
void |
onChanged(int position,
int count)
Called by the SortedList when the item at the given position is updated.
|
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. |
onChanged
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public BatchedCallback(SortedList.Callback<T2> wrappedCallback)
wrappedCallback
- The Callback which should received the data change callbacks.
Other method calls (e.g. compare(Object, Object)
from
the SortedList are directly forwarded to this Callback.public int compare(T2 o1, T2 o2)
SortedList.Callback
Comparator.compare(Object, Object)
, should compare two and
return how they should be ordered.compare
in interface Comparator<T2>
compare
in class SortedList.Callback<T2>
o1
- The first object to compare.o2
- The second object to compare.public void onInserted(int position, int count)
ListUpdateCallback
count
number of items are inserted at the given position.position
- The position of the new item.count
- The number of items that have been added.public void onRemoved(int position, int count)
ListUpdateCallback
count
number of items are removed from the given position.position
- The position of the item which has been removed.count
- The number of items which have been removed.public void onMoved(int fromPosition, int toPosition)
ListUpdateCallback
fromPosition
- The previous position of the item before the move.toPosition
- The new position of the item.public void onChanged(int position, int count)
SortedList.Callback
onChanged
in class SortedList.Callback<T2>
position
- The position of the item which has been updated.count
- The number of items which has changed.public boolean areContentsTheSame(T2 oldItem, T2 newItem)
SortedList.Callback
SortedList.Callback.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.
areContentsTheSame
in class SortedList.Callback<T2>
oldItem
- The previous representation of the object.newItem
- The new object that replaces the previous one.public boolean areItemsTheSame(T2 item1, T2 item2)
SortedList.Callback
For example, if your items have unique ids, this method should check their equality.
areItemsTheSame
in class SortedList.Callback<T2>
item1
- The first item to check.item2
- The second item to check.public void dispatchLastEvent()