-
public class DiffUtil.DiffResult
This class holds the information about the result of a calculateDiff call.
You can consume the updates in a DiffResult via dispatchUpdatesTo or directly stream the results into a RecyclerView.Adapter via dispatchUpdatesTo.
-
-
Field Summary
Fields Modifier and Type Field Description public final static int
NO_POSITION
-
Method Summary
Modifier and Type Method Description int
convertOldPositionToNew(int oldListPosition)
Given a position in the old list, returns the position in the new list, or {@code NO_POSITION}
if it was removed.int
convertNewPositionToOld(int newListPosition)
Given a position in the new list, returns the position in the old list, or {@code NO_POSITION}
if it was removed.void
dispatchUpdatesTo(@NonNull() RecyclerView.Adapter adapter)
Dispatches the update events to the given adapter. void
dispatchUpdatesTo(@NonNull() ListUpdateCallback updateCallback)
Dispatches update operations to the given Callback. -
-
Method Detail
-
convertOldPositionToNew
int convertOldPositionToNew(int oldListPosition)
Given a position in the old list, returns the position in the new list, or
{@code NO_POSITION}
if it was removed.- Parameters:
oldListPosition
- Position of item in old list
-
convertNewPositionToOld
int convertNewPositionToOld(int newListPosition)
Given a position in the new list, returns the position in the old list, or
{@code NO_POSITION}
if it was removed.- Parameters:
newListPosition
- Position of item in new list
-
dispatchUpdatesTo
void dispatchUpdatesTo(@NonNull() RecyclerView.Adapter adapter)
Dispatches the update events to the given adapter.
For example, if you have an Adapter that is backed by a List, you can swap the list with the new one then call thismethod to dispatch all updates to the RecyclerView.
List oldList = mAdapter.getData(); DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList)); mAdapter.setData(newList); result.dispatchUpdatesTo(mAdapter);
Note that the RecyclerView requires you to dispatch adapter updates immediately when youchange the data (you cannot defer
{@code notify*}
calls). The usage above adheres to thisrule because updates are sent to the adapter right after the backing data is changed,before RecyclerView tries to read it.On the other hand, if you have another AdapterDataObserver that tries to process events synchronously, this may confuse that observer because thelist is instantly moved to its final state while the adapter updates are dispatched lateron, one by one. If you have such an AdapterDataObserver,you can use dispatchUpdatesTo to handle each modificationmanually.
- Parameters:
adapter
- A RecyclerView adapter which was displaying the old list and will startdisplaying the new list.
-
dispatchUpdatesTo
void dispatchUpdatesTo(@NonNull() ListUpdateCallback updateCallback)
Dispatches update operations to the given Callback.
These updates are atomic such that the first update call affects every update call thatcomes after it (the same as RecyclerView).
- Parameters:
updateCallback
- The callback to receive the update operations.
-
-
-
-