Java Code Examples for androidx.recyclerview.widget.DiffUtil#DiffResult

The following examples show how to use androidx.recyclerview.widget.DiffUtil#DiffResult . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColumnSortHandler.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void swapItems(List<List<ISortableModel>> newItems, int column) {

        List<List<ISortableModel>> oldItems = (List<List<ISortableModel>>)
                mCellRecyclerViewAdapter.getItems();

        // Set new items without calling notifyCellDataSetChanged method of CellRecyclerViewAdapter
        mCellRecyclerViewAdapter.setItems(newItems, !mEnableAnimation);

        if(mEnableAnimation) {
            // Find the differences between old cell items and new items.
            final ColumnSortCallback diffCallback = new ColumnSortCallback(oldItems, newItems, column);
            final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

            diffResult.dispatchUpdatesTo(mCellRecyclerViewAdapter);
            diffResult.dispatchUpdatesTo(mRowHeaderRecyclerViewAdapter);
        }

    }
 
Example 2
Source File: RecyclerViewAdapterHelper.java    From MultiTypeRecyclerViewAdapter with Apache License 2.0 6 votes vote down vote up
/**
 * 处理数据,通知RV刷新
 *
 * @param diffResult 返回的diffResult
 */
protected final void handleResult(DiffUtil.DiffResult diffResult) {
    checkAdapterBind();

    diffResult.dispatchUpdatesTo(getListUpdateCallback(mAdapter));

    mData.clear();
    mData.addAll(mNewData);

    HandleBase<T> pollData = mRefreshQueue.poll();
    if (pollData != null) {
        startRefresh(pollData);
    } else {
        onEnd();
    }
}
 
Example 3
Source File: ItemProvider.java    From AdapterDelegates with Apache License 2.0 6 votes vote down vote up
/**
 * @return null if removement cant be executed because min list size is required
 */
@Nullable
public Pair<List<Item>, DiffUtil.DiffResult> remove() {
    if (currentList.size() <= 6) {
        return null;
    }
    List<Item> newlist = copyCurrent();

    newlist.remove(1);
    newlist.remove(3);

    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new ItemDiff(currentList, newlist));

    currentList = newlist;
    return Pair.create(newlist, diffResult);
}
 
Example 4
Source File: ItemProvider.java    From AdapterDelegates with Apache License 2.0 6 votes vote down vote up
public Pair<List<Item>, DiffUtil.DiffResult> modify() {
    List<Item> newlist = copyCurrent();

    Item c1 = newlist.get(0).copy();
    c1.color = color();
    newlist.set(0, c1);

    Item c2 = newlist.get(5).copy();
    c2.text = c2.text + " - Updated";
    newlist.set(5, c2);

    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new ItemDiff(currentList, newlist));

    currentList = newlist;
    return Pair.create(newlist, diffResult);
}
 
Example 5
Source File: ColumnSortHandler.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void swapItems(List<List<ISortableModel>> oldItems, List<List<ISortableModel>>
        newItems, int column, List<ISortableModel> newRowHeader, SortState sortState) {

    // Set new items without calling notifyCellDataSetChanged method of CellRecyclerViewAdapter
    mCellRecyclerViewAdapter.setItems(newItems, !mEnableAnimation);
    mRowHeaderRecyclerViewAdapter.setItems(newRowHeader, !mEnableAnimation);

    if(mEnableAnimation) {
        // Find the differences between old cell items and new items.
        final ColumnSortCallback diffCallback = new ColumnSortCallback(oldItems, newItems, column);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        diffResult.dispatchUpdatesTo(mCellRecyclerViewAdapter);
        diffResult.dispatchUpdatesTo(mRowHeaderRecyclerViewAdapter);
    }

    for (ColumnSortStateChangedListener listener : columnSortStateChangedListeners) {
        listener.onColumnSortStatusChanged(column, sortState);
    }
}
 
Example 6
Source File: ColumnSortHandler.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void swapItems(List<ISortableModel> oldRowHeader,
                       List<ISortableModel> newRowHeader,
                       List<List<ISortableModel>> newColumnItems,
                       SortState sortState
) {

    // Set new items without calling notifyCellDataSetChanged method of CellRecyclerViewAdapter
    mRowHeaderRecyclerViewAdapter.setItems(newRowHeader, !mEnableAnimation);
    mCellRecyclerViewAdapter.setItems(newColumnItems, !mEnableAnimation);

    if(mEnableAnimation) {
        // Find the differences between old cell items and new items.
        final RowHeaderSortCallback diffCallback = new RowHeaderSortCallback(oldRowHeader, newRowHeader);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        diffResult.dispatchUpdatesTo(mRowHeaderRecyclerViewAdapter);
        diffResult.dispatchUpdatesTo(mCellRecyclerViewAdapter);
    }

    for (ColumnSortStateChangedListener listener : columnSortStateChangedListeners) {
        listener.onRowHeaderSortStatusChanged(sortState);
    }
}
 
Example 7
Source File: BankTransferActivity.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
private void refreshTransfer() {
    try {
        UserEntity userEntity = sDataManager.getTradeBean().getUsers().get(sDataManager.USER_ID);
        if (userEntity == null) return;
        mNewData.clear();
        for (TransferEntity transferEntity :
                userEntity.getTransfers().values()) {
            TransferEntity t = CloneUtils.clone(transferEntity);
            mNewData.add(t);
        }
        Collections.sort(mNewData);
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new TransferDiffCallback(mOldData, mNewData), false);
        mAdapter.setData(mNewData);
        diffResult.dispatchUpdatesTo(mAdapter);
        mOldData.clear();
        mOldData.addAll(mNewData);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 8
Source File: ColumnSortHandler.java    From TableView with MIT License 6 votes vote down vote up
private void swapItems(@NonNull List<List<ISortableModel>> oldItems, @NonNull List<List<ISortableModel>>
        newItems, int column, @NonNull List<ISortableModel> newRowHeader, @NonNull SortState sortState) {

    // Set new items without calling notifyCellDataSetChanged method of CellRecyclerViewAdapter
    mCellRecyclerViewAdapter.setItems(newItems, !mEnableAnimation);
    mRowHeaderRecyclerViewAdapter.setItems(newRowHeader, !mEnableAnimation);

    if (mEnableAnimation) {
        // Find the differences between old cell items and new items.
        final ColumnSortCallback diffCallback = new ColumnSortCallback(oldItems, newItems, column);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        diffResult.dispatchUpdatesTo(mCellRecyclerViewAdapter);
        diffResult.dispatchUpdatesTo(mRowHeaderRecyclerViewAdapter);
    }

    for (ColumnSortStateChangedListener listener : columnSortStateChangedListeners) {
        listener.onColumnSortStatusChanged(column, sortState);
    }
}
 
Example 9
Source File: DiffResult.java    From epoxy with Apache License 2.0 5 votes vote down vote up
private DiffResult(
    @NonNull List<? extends EpoxyModel<?>> previousModels,
    @NonNull List<? extends EpoxyModel<?>> newModels,
    @Nullable DiffUtil.DiffResult differResult
) {
  this.previousModels = previousModels;
  this.newModels = newModels;
  this.differResult = differResult;
}
 
Example 10
Source File: GroupMemberListAdapter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
void updateData(@NonNull List<? extends GroupMemberEntry> recipients) {
  if (data.isEmpty()) {
    data.addAll(recipients);
    notifyDataSetChanged();
  } else {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallback(data, recipients));
    data.clear();
    data.addAll(recipients);
    diffResult.dispatchUpdatesTo(this);
  }
}
 
Example 11
Source File: BreadcrumbsView.java    From BreadcrumbsView with MIT License 5 votes vote down vote up
/**
 * Set breadcrumb items list and animates them correctly with recyclerview diff
 *
 * @param items Target list
 */
public <E extends IBreadcrumbItem> void updateItems(@NonNull List<E> items) {
	DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new BreadcrumbsDiffCallback((List<IBreadcrumbItem>)items, mAdapter.getItems()));
	mAdapter.setItems(items);
	diffResult.dispatchUpdatesTo(mAdapter);
	postScroll(-1, 0);
}
 
Example 12
Source File: SpendingRecyclerViewAdapter.java    From Moneycim with MIT License 5 votes vote down vote up
public void updateItems(List<Spending> spendings){
    final SpendingDiffCallback diffCallback = new SpendingDiffCallback(this.spendings, spendings);
    final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

    this.spendings.clear();
    this.spendings.addAll(spendings);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 13
Source File: ItemProvider.java    From AdapterDelegates with Apache License 2.0 5 votes vote down vote up
public Pair<List<Item>, DiffUtil.DiffResult> move() {
    List<Item> newlist = copyCurrent();

    Item c2 = newlist.remove(5);
    c2.color = color();
    newlist.add(1, c2);

    DiffUtil.DiffResult diffResult =
            DiffUtil.calculateDiff(new ItemDiff(currentList, newlist), true);

    currentList = newlist;
    return Pair.create(newlist, diffResult);
}
 
Example 14
Source File: GamesAdapter.java    From UpdogFarmer with GNU General Public License v3.0 5 votes vote down vote up
private void applyDiffResult(List<Game> games, DiffUtil.DiffResult diffResult) {
    pendingUpdates.remove(games);
    dispatchUpdates(games, diffResult);
    if (pendingUpdates.size() > 0) {
        final List<Game> latest = pendingUpdates.pop();
        pendingUpdates.clear();
        updateDataInternal(latest);
    }
}
 
Example 15
Source File: ItemProvider.java    From AdapterDelegates with Apache License 2.0 5 votes vote down vote up
public Pair<List<Item>, DiffUtil.DiffResult> add() {
    List<Item> newlist = copyCurrent();

    newlist.add(2, newItem());
    newlist.add(4, newItem());

    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new ItemDiff(currentList, newlist));

    currentList = newlist;
    return Pair.create(newlist, diffResult);
}
 
Example 16
Source File: FragmentOptionsSynchronize.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
public void set(@NonNull List<EntityAccount> accounts) {
    Log.i("Set accounts=" + accounts.size());

    DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, accounts), false);
    items = accounts;
    diff.dispatchUpdatesTo(this);
}
 
Example 17
Source File: AnimationDiffUtilsActivity.java    From AdapterDelegates with Apache License 2.0 4 votes vote down vote up
private void updateItems() {
    Pair<List<Item>, DiffUtil.DiffResult> modify = itemProvider.modify();

    adapter.setItems(modify.first);
    modify.second.dispatchUpdatesTo(adapter);
}
 
Example 18
Source File: AccountHistoryAdapter.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void updateList(List<AccountHistoryResponseItem> newList) {
    List<AccountHistoryResponseItem> oldList = this.historyList;
    this.historyList = newList;
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AccountHistoryDiffCallback(oldList, newList), true);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 19
Source File: ContactOverviewSelectionAdapter.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void updateList(List<Contact> newList) {
    List<Contact> oldList = this.contactList;
    this.contactList = newList;
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new ContactSelectionDiffCallback(oldList, newList), false);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 20
Source File: PlayersAdapter.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void dispatchUpdate(@NonNull DiffUtil.DiffResult result) {
    result.dispatchUpdatesTo(this);
}