background
RecyclerView.Adapter#notifyDataSetChanged()
Every time will refresh the entire layout;- Each time manually invoke
RecyclerView.Adapter#notifyItemXx
series method is cumbersome; - Repeat if necessary with the old list item in the new project, only updated content, rather than inserting duplicates.
DiffUtil
DiffUtil
It is to simplify the operation to update data RecyclerVeiw born. The key classes and methods are as follows:
class | method | description |
---|---|---|
DiffUtil | public static DiffResult calculateDiff(Callback cb) | The analysis of the data provided by the difference Callback old and new data list, return DiffResult |
DiffUtil.Callback | public abstract int getOldListSize() | Returns the number of old data |
public abstract int getNewListSize() | Returns the number of new data | |
public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition) | Decide whether two data items are the same | |
public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition) | (When the two data items are the same, the) decides whether the contents of two data items of the same, or need to be updated | |
DiffUtil.DiffResult | public void dispatchUpdatesTo(final RecyclerView.Adapter adapter) | The difference in updates to RecyclerView.Adapter |
step
- Creating a class that implements
DiffUtil.Callback
; - When the arrival of new data, callback instantiates custom, old and new incoming transactions;
- In the thread of the call
DiffUtil#calculateDiff
calculate the difference; - The differences in the results
DiffResult
updated to RecyclerView.Adapter in.
More
- Use
android.support.v7.recyclerview.extensions.ListAdapter
andandroid.support.v7.recyclerview.extensions.AsyncListDiffer