Package 

Class SortedList.BatchedCallback

  • All Implemented Interfaces:
    java.util.Comparator , tds.androidx.recyclerview.widget.ListUpdateCallback

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

    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.

    • Method Summary

      Modifier and Type Method Description
      int compare(T2 o1, T2 o2) Similar to compare, should compare two andreturn how they should be ordered.
      void onInserted(int position, int count) Called when {@code count} number of items are inserted at the given position.
      void onRemoved(int position, int count) Called when {@code count} number of items are removed from the given position.
      void onMoved(int fromPosition, int toPosition) Called when an item changes its position in the list.
      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) Called when {@code count} number of items are updated at the given position.
      boolean areContentsTheSame(T2 oldItem, T2 newItem) Called by the SortedList when it wants to check whether two items have the same dataor not.
      boolean areItemsTheSame(T2 item1, T2 item2) Called by the SortedList to decide whether two objects represent the same Item or not.
      Object getChangePayload(T2 item1, T2 item2) When areItemsTheSame returns {@code true} for two items and areContentsTheSame returns false for them, Callback calls thismethod to get a payload about the change.
      void dispatchLastEvent() This method dispatches any pending event notifications to the wrapped Callback.
      • Methods inherited from class java.util.Comparator

        compare, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reverseOrder, reversed, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
      • Methods inherited from class tds.androidx.recyclerview.widget.ListUpdateCallback

        onChanged
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SortedList.BatchedCallback

        SortedList.BatchedCallback(SortedList.Callback<T2> wrappedCallback)
        Creates a new BatchedCallback that wraps the provided Callback.
        Parameters:
        wrappedCallback - The Callback which should received the data change callbacks.Other method calls (e.g.
    • Method Detail

      • compare

         int compare(T2 o1, T2 o2)

        Similar to compare, should compare two andreturn how they should be ordered.

        Parameters:
        o1 - The first object to compare.
        o2 - The second object to compare.
      • onInserted

         void onInserted(int position, int count)

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

        Parameters:
        position - The position of the new item.
        count - The number of items that have been added.
      • onRemoved

         void onRemoved(int position, int count)

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

        Parameters:
        position - The position of the item which has been removed.
        count - The number of items which have been removed.
      • onMoved

         void onMoved(int fromPosition, int toPosition)

        Called when an item changes its position in the list.

        Parameters:
        fromPosition - The previous position of the item before the move.
        toPosition - The new position of the item.
      • onChanged

         void onChanged(int position, int count)

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

        Parameters:
        position - The position of the item which has been updated.
        count - The number of items which has changed.
      • onChanged

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

        Called when {@code count} number of items are updated at the given position.

        Parameters:
        position - The position of the item which has been updated.
        count - The number of items which has changed.
      • areContentsTheSame

         boolean areContentsTheSame(T2 oldItem, T2 newItem)

        Called by the SortedList when it wants to check whether two items have the same dataor not. SortedList uses this information to decide whether it should call onChanged or not.

        SortedList uses this method to check equality instead of equals sothat you can change its behavior depending on your UI.

        For example, if you are using SortedList with a RecyclerView.Adapter, you shouldreturn whether the items' visual representations are the same or not.

        Parameters:
        oldItem - The previous representation of the object.
        newItem - The new object that replaces the previous one.
      • areItemsTheSame

         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.

        Parameters:
        item1 - The first item to check.
        item2 - The second item to check.
      • getChangePayload

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

        When areItemsTheSame returns {@code true} for two items and areContentsTheSame returns false for them, Callback calls thismethod to get a payload about the change.

        For example, if you are using Callback with RecyclerView, you can return the particular field thatchanged in the item and your ItemAnimator can use thatinformation to run the correct animation.

        Default implementation returns {@code null}.

        Parameters:
        item1 - The first item to check.
        item2 - The second item to check.
      • dispatchLastEvent

         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.