public class

DiffUtil.DiffResult

extends java.lang.Object

 java.lang.Object

↳androidx.recyclerview.widget.DiffUtil.DiffResult

Overview

This class holds the information about the result of a DiffUtil.calculateDiff(DiffUtil.Callback, boolean) call.

You can consume the updates in a DiffResult via DiffUtil.DiffResult.dispatchUpdatesTo(ListUpdateCallback) or directly stream the results into a RecyclerView.Adapter via DiffUtil.DiffResult.dispatchUpdatesTo(RecyclerView.Adapter).

Summary

Fields
public static final intNO_POSITION

Signifies an item not present in the list.

Methods
public intconvertNewPositionToOld(int newListPosition)

Given a position in the new list, returns the position in the old list, or NO_POSITION if it was removed.

public intconvertOldPositionToNew(int oldListPosition)

Given a position in the old list, returns the position in the new list, or NO_POSITION if it was removed.

public voiddispatchUpdatesTo(ListUpdateCallback updateCallback)

Dispatches update operations to the given Callback.

public voiddispatchUpdatesTo(RecyclerView.Adapter adapter)

Dispatches the update events to the given adapter.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int NO_POSITION

Signifies an item not present in the list.

Methods

public int convertOldPositionToNew(int oldListPosition)

Given a position in the old list, returns the position in the new list, or NO_POSITION if it was removed.

Parameters:

oldListPosition: Position of item in old list

Returns:

Position of item in new list, or NO_POSITION if not present.

See also: DiffUtil.DiffResult.NO_POSITION, DiffUtil.DiffResult.convertNewPositionToOld(int)

public int convertNewPositionToOld(int newListPosition)

Given a position in the new list, returns the position in the old list, or NO_POSITION if it was removed.

Parameters:

newListPosition: Position of item in new list

Returns:

Position of item in old list, or NO_POSITION if not present.

See also: DiffUtil.DiffResult.NO_POSITION, DiffUtil.DiffResult.convertOldPositionToNew(int)

public void dispatchUpdatesTo(RecyclerView.Adapter adapter)

Dispatches the update events to the given adapter.

For example, if you have an Adapter that is backed by a java.util.List, you can swap the list with the new one then call this method 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 you change the data (you cannot defer notify* calls). The usage above adheres to this rule 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 the list is instantly moved to its final state while the adapter updates are dispatched later on, one by one. If you have such an AdapterDataObserver, you can use DiffUtil.DiffResult.dispatchUpdatesTo(ListUpdateCallback) to handle each modification manually.

Parameters:

adapter: A RecyclerView adapter which was displaying the old list and will start displaying the new list.

See also: AdapterListUpdateCallback

public void dispatchUpdatesTo(ListUpdateCallback updateCallback)

Dispatches update operations to the given Callback.

These updates are atomic such that the first update call affects every update call that comes after it (the same as RecyclerView).

Parameters:

updateCallback: The callback to receive the update operations.

See also: DiffUtil.DiffResult.dispatchUpdatesTo(RecyclerView.Adapter)