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 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
dispatchUpdatesTo(ListUpdateCallback) to handle each modification
manually.
Parameters
adapter
Adapter: A RecyclerView adapter which was displaying the old list and will start
displaying the new list.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# DiffUtil.DiffResult\n\nadded in [version 25.1.0](/topic/libraries/support-library/revisions) \nbelongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1 \nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \\| [\\[Expand All\\]](#) \n\nDiffUtil.DiffResult\n===================\n\n| This package is part of the Android [support library](/topic/libraries/support-library) which is no longer maintained. The support library has been superseded by [AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider [migrating](/jetpack/androidx/migrate) existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library [class\n| mappings](/jetpack/androidx/migrate/class-mappings).\n\n\n`\npublic\nstatic\n\n\nclass\nDiffUtil.DiffResult\n`\n\n\n`\n\nextends Object\n\n\n`\n\n`\n\n\n`\n\n|---|---------------------------------------------|\n| java.lang.Object ||\n| ↳ | android.support.v7.util.DiffUtil.DiffResult |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nThis class holds the information about the result of a\n[calculateDiff(Callback, boolean)](/reference/android/support/v7/util/DiffUtil#calculateDiff(android.support.v7.util.DiffUtil.Callback, boolean)) call.\n\n\nYou can consume the updates in a DiffResult via\n[dispatchUpdatesTo(ListUpdateCallback)](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.util.ListUpdateCallback)) or directly stream the results into a\n[RecyclerView.Adapter](/reference/android/support/v7/widget/RecyclerView.Adapter) via [dispatchUpdatesTo(RecyclerView.Adapter)](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.widget.RecyclerView.Adapter)).\n\nSummary\n-------\n\n| ### Public methods ||\n|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` void` | ` `[dispatchUpdatesTo](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.util.ListUpdateCallback))`(`[ListUpdateCallback](/reference/android/support/v7/util/ListUpdateCallback)` updateCallback) ` Dispatches update operations to the given Callback. |\n| ` void` | ` `[dispatchUpdatesTo](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.widget.RecyclerView.Adapter))`(`[Adapter](/reference/android/support/v7/widget/RecyclerView.Adapter)` adapter) ` Dispatches the update events to the given adapter. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` java.lang.Object ` |-------------------|-------------------------------| | ` Object` | ` clone() ` | | ` boolean` | ` equals(Object arg0) ` | | ` void` | ` finalize() ` | | ` final Class\u003c?\u003e` | ` getClass() ` | | ` int` | ` hashCode() ` | | ` final void` | ` notify() ` | | ` final void` | ` notifyAll() ` | | ` String` | ` toString() ` | | ` final void` | ` wait(long arg0, int arg1) ` | | ` final void` | ` wait(long arg0) ` | | ` final void` | ` wait() ` | ||\n\nPublic methods\n--------------\n\n### dispatchUpdatesTo\n\nadded in [version 25.1.0](/topic/libraries/support-library/revisions) \n\n```\nvoid dispatchUpdatesTo (ListUpdateCallback updateCallback)\n```\n\nDispatches update operations to the given Callback.\n\n\nThese updates are atomic such that the first update call affects every update call that\ncomes after it (the same as RecyclerView).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------------|-----------------------------------------------------------------------------|\n| `updateCallback` | `ListUpdateCallback`: The callback to receive the update operations. \u003cbr /\u003e |\n\n**See also:**\n\n- [dispatchUpdatesTo(RecyclerView.Adapter)](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.widget.RecyclerView.Adapter)) \n\n### dispatchUpdatesTo\n\nadded in [version 25.1.0](/topic/libraries/support-library/revisions) \n\n```\nvoid dispatchUpdatesTo (Adapter adapter)\n```\n\nDispatches the update events to the given adapter.\n\n\nFor example, if you have an [Adapter](/reference/android/support/v7/widget/RecyclerView.Adapter)\nthat is backed by a [List](/reference/java/util/List), you can swap the list with the new one then call this\nmethod to dispatch all updates to the RecyclerView. \n\n```\n List oldList = mAdapter.getData();\n DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));\n mAdapter.setData(newList);\n result.dispatchUpdatesTo(mAdapter);\n \n```\n\n\nNote that the RecyclerView requires you to dispatch adapter updates immediately when you\nchange the data (you cannot defer `notify*` calls). The usage above adheres to this\nrule because updates are sent to the adapter right after the backing data is changed,\nbefore RecyclerView tries to read it.\n\n\nOn the other hand, if you have another\n[AdapterDataObserver](/reference/android/support/v7/widget/RecyclerView.AdapterDataObserver)\nthat tries to process events synchronously, this may confuse that observer because the\nlist is instantly moved to its final state while the adapter updates are dispatched later\non, one by one. If you have such an\n[AdapterDataObserver](/reference/android/support/v7/widget/RecyclerView.AdapterDataObserver),\nyou can use\n[dispatchUpdatesTo(ListUpdateCallback)](/reference/android/support/v7/util/DiffUtil.DiffResult#dispatchUpdatesTo(android.support.v7.util.ListUpdateCallback)) to handle each modification\nmanually.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----------|--------------------------------------------------------------------------------------------------------------------|\n| `adapter` | `Adapter`: A RecyclerView adapter which was displaying the old list and will start displaying the new list. \u003cbr /\u003e |\n\n**See also:**\n\n- [AdapterListUpdateCallback](/reference/android/support/v7/util/AdapterListUpdateCallback) \n-\n\n Interfaces\n ----------\n\n - [ListUpdateCallback](/reference/android/support/v7/util/ListUpdateCallback)\n-\n\n Classes\n -------\n\n - [AdapterListUpdateCallback](/reference/android/support/v7/util/AdapterListUpdateCallback)\n - [AsyncListUtil](/reference/android/support/v7/util/AsyncListUtil)\n - [AsyncListUtil.DataCallback](/reference/android/support/v7/util/AsyncListUtil.DataCallback)\n - [AsyncListUtil.ViewCallback](/reference/android/support/v7/util/AsyncListUtil.ViewCallback)\n - [BatchingListUpdateCallback](/reference/android/support/v7/util/BatchingListUpdateCallback)\n - [DiffUtil](/reference/android/support/v7/util/DiffUtil)\n - [DiffUtil.Callback](/reference/android/support/v7/util/DiffUtil.Callback)\n - [DiffUtil.DiffResult](/reference/android/support/v7/util/DiffUtil.DiffResult)\n - [DiffUtil.ItemCallback](/reference/android/support/v7/util/DiffUtil.ItemCallback)\n - [SortedList](/reference/android/support/v7/util/SortedList)\n - [SortedList.BatchedCallback](/reference/android/support/v7/util/SortedList.BatchedCallback)\n - [SortedList.Callback](/reference/android/support/v7/util/SortedList.Callback)"]]