Search in sources :

Example 1 with ListUpdateCallback

use of androidx.recyclerview.widget.ListUpdateCallback in project materialistic by hidroh.

the class StoryRecyclerViewAdapter method setUpdated.

private void setUpdated(Item[] items) {
    if (!mHighlightUpdated || getItems() == null) {
        return;
    }
    if (mItems.size() == 0) {
        return;
    }
    mAdded.clear();
    mPromoted.clear();
    DiffUtil.calculateDiff(new DiffUtil.Callback() {

        @Override
        public int getOldListSize() {
            return mItems.size();
        }

        @Override
        public int getNewListSize() {
            return items.length;
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return mItems.get(oldItemPosition).getLongId() == items[newItemPosition].getLongId();
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return areItemsTheSame(oldItemPosition, newItemPosition);
        }
    }).dispatchUpdatesTo(new ListUpdateCallback() {

        @Override
        public void onInserted(int position, int count) {
            mAdded.add(items[position]);
            notifyUpdated();
        }

        @Override
        public void onRemoved(int position, int count) {
        // no-op
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
            if (toPosition < fromPosition) {
                mPromoted.put(mItems.get(fromPosition).getId(), fromPosition - toPosition);
            }
        }

        @Override
        public void onChanged(int position, int count, Object payload) {
        // no-op
        }
    });
}
Also used : ListUpdateCallback(androidx.recyclerview.widget.ListUpdateCallback) SortedListAdapterCallback(androidx.recyclerview.widget.SortedListAdapterCallback) ListUpdateCallback(androidx.recyclerview.widget.ListUpdateCallback) SuppressLint(android.annotation.SuppressLint)

Example 2 with ListUpdateCallback

use of androidx.recyclerview.widget.ListUpdateCallback in project SherlockAdapter by EvilBT.

the class BaseAdapter method setData.

/**
 * 设置新数据,会清除掉原有数据,并有可能重置加载更多状态
 * @param data 数据集合
 */
public void setData(@Nullable List<? extends T> data) {
    if (mOpenAutoLoadMore) {
        mLoadState = LOADING;
        mIsLoading = false;
    }
    mShowErrorView = false;
    if (mCallback == null) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        doNotifyDataSetChanged();
        return;
    }
    final List<T> oldData = new ArrayList<>(mData);
    mData.clear();
    if (data != null) {
        mData.addAll(data);
    }
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {

        @Override
        public int getOldListSize() {
            return oldData.size();
        }

        @Override
        public int getNewListSize() {
            return mData.size();
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            final T oldItem = oldData.get(oldItemPosition);
            final T newItem = mData.get(newItemPosition);
            return mCallback.areItemsTheSame(oldItem, newItem);
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            final T oldItem = oldData.get(oldItemPosition);
            final T newItem = mData.get(newItemPosition);
            return mCallback.areContentsTheSame(oldItem, newItem);
        }

        @Nullable
        @Override
        public Object getChangePayload(int oldItemPosition, int newItemPosition) {
            final T oldItem = oldData.get(oldItemPosition);
            final T newItem = mData.get(newItemPosition);
            return mCallback.getChangePayload(oldItem, newItem);
        }
    }, mDetectMoves);
    result.dispatchUpdatesTo(new ListUpdateCallback() {

        @Override
        public void onInserted(int position, int count) {
            int skew = getHeadSize();
            doNotifyItemRangeInserted(position + skew, count);
        }

        @Override
        public void onRemoved(int position, int count) {
            int skew = getHeadSize();
            doNotifyItemRangeRemoved(position + skew, count);
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
            int skew = getHeadSize();
            doNotifyItemMoved(fromPosition + skew, toPosition + skew);
        }

        @Override
        public void onChanged(int position, int count, Object payload) {
            int skew = getHeadSize();
            doNotifyItemRangeChanged(position + skew, count, payload);
        }
    });
}
Also used : ListUpdateCallback(androidx.recyclerview.widget.ListUpdateCallback) DiffUtil(androidx.recyclerview.widget.DiffUtil) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Example 3 with ListUpdateCallback

use of androidx.recyclerview.widget.ListUpdateCallback in project SherlockAdapter by EvilBT.

the class ExpandableAdapter method setData.

/**
 * 设置新数据,会清除掉原有数据,并有可能重置加载更多状态
 * @param data 数据集合
 */
public void setData(@Nullable List<? extends IMultiItem> data) {
    if (mOpenAutoLoadMore) {
        mLoadState = LOADING;
        mIsLoading = false;
    }
    mShowErrorView = false;
    if (mCallback == null) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        doNotifyDataSetChanged();
        return;
    }
    final List<IMultiItem> oldData = new ArrayList<>(mData);
    mData.clear();
    if (data != null) {
        mData.addAll(data);
    }
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {

        @Override
        public int getOldListSize() {
            return getShowSize(oldData);
        }

        @Override
        public int getNewListSize() {
            return getShowSize(mData);
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            final IMultiItem oldItem = getData(oldData, oldItemPosition);
            final IMultiItem newItem = getData(mData, newItemPosition);
            return mCallback.areItemsTheSame(oldItem, newItem);
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            final IMultiItem oldItem = getData(oldData, oldItemPosition);
            final IMultiItem newItem = getData(mData, newItemPosition);
            return mCallback.areContentsTheSame(oldItem, newItem);
        }

        @Nullable
        @Override
        public Object getChangePayload(int oldItemPosition, int newItemPosition) {
            final IMultiItem oldItem = getData(oldData, oldItemPosition);
            final IMultiItem newItem = getData(mData, newItemPosition);
            return mCallback.getChangePayload(oldItem, newItem);
        }
    }, mDetectMoves);
    result.dispatchUpdatesTo(new ListUpdateCallback() {

        @Override
        public void onInserted(int position, int count) {
            int skew = getHeadSize();
            doNotifyItemRangeInserted(position + skew, count);
        }

        @Override
        public void onRemoved(int position, int count) {
            int skew = getHeadSize();
            doNotifyItemRangeRemoved(position + skew, count);
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
            int skew = getHeadSize();
            doNotifyItemMoved(fromPosition + skew, toPosition + skew);
        }

        @Override
        public void onChanged(int position, int count, Object payload) {
            int skew = getHeadSize();
            doNotifyItemRangeChanged(position + skew, count, payload);
        }
    });
}
Also used : ListUpdateCallback(androidx.recyclerview.widget.ListUpdateCallback) DiffUtil(androidx.recyclerview.widget.DiffUtil) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Example 4 with ListUpdateCallback

use of androidx.recyclerview.widget.ListUpdateCallback in project LibTest by liudao01.

the class HoverViewStateExpanded method setMenu.

@Override
public void setMenu(@Nullable HoverMenu menu) {
    Log.d(TAG, "Setting menu.");
    mHoverView.mMenu = menu;
    // Expanded menus can't be null/empty.  If it is then go to closed state.
    if (null == mHoverView.mMenu || mHoverView.mMenu.getSectionCount() == 0) {
        close();
        return;
    }
    mHoverView.restoreVisualState();
    if (null == mHoverView.mSelectedSectionId || null == mHoverView.mMenu.getSection(mHoverView.mSelectedSectionId)) {
        mHoverView.mSelectedSectionId = mHoverView.mMenu.getSection(0).getId();
    }
    mHoverView.mMenu.setUpdatedCallback(new ListUpdateCallback() {

        @Override
        public void onInserted(int position, int count) {
            Log.d(TAG, "onInserted. Position: " + position + ", Count: " + count);
            int[] sectionIndices = new int[count];
            for (int i = position; i < position + count; ++i) {
                sectionIndices[i - position] = i;
            }
            createTabsForIndices(sectionIndices);
        }

        @Override
        public void onRemoved(int position, int count) {
            Log.d(TAG, "onRemoved. Position: " + position + ", Count: " + count);
            int[] sectionIndices = new int[count];
            for (int i = position; i < position + count; ++i) {
                sectionIndices[i - position] = i;
            }
            removeSections(sectionIndices);
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
            Log.d(TAG, "onMoved from: " + fromPosition + ", to: " + toPosition);
            reorderSection(fromPosition, toPosition);
        }

        @Override
        public void onChanged(int position, int count, Object payload) {
            Log.d(TAG, "Tab(s) changed. From: " + position + ", To: " + count);
            int[] sectionIndices = new int[count];
            for (int i = position; i < position + count; ++i) {
                sectionIndices[i - position] = i;
            }
            updateSections(sectionIndices);
        }
    });
    if (mHasControl && !mHasMenu) {
        Log.d(TAG, "Has control.  Received initial menu.  Expanding menu.");
        expandMenu();
    } else if (mHasControl) {
        Log.d(TAG, "Has control.  Already had menu.  Switching menu.");
        transitionDisplayFromOldMenuToNew();
    }
    mHasMenu = true;
}
Also used : ListUpdateCallback(androidx.recyclerview.widget.ListUpdateCallback) Point(android.graphics.Point)

Aggregations

ListUpdateCallback (androidx.recyclerview.widget.ListUpdateCallback)4 Nullable (androidx.annotation.Nullable)2 DiffUtil (androidx.recyclerview.widget.DiffUtil)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 Point (android.graphics.Point)1 SortedListAdapterCallback (androidx.recyclerview.widget.SortedListAdapterCallback)1