public void setItems()

in LeanbackShowcase/app/src/main/java/androidx/leanback/leanbackshowcase/app/room/adapter/ListAdapter.java [208:285]


    public void setItems(final List<T> itemList, final Comparator<T> sameItemComparator,
                         final Comparator<T> sameContentComparator) {
        if (DEBUG) {
            Log.e(TAG, "new items: " + itemList);
            Log.e(TAG, "old items: " + mItems);
        }

        DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
            @Override
            public int getOldListSize() {
                return mItems.size();
            }

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

            @Override
            public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                return sameItemComparator.compare(mItems.get(oldItemPosition),
                        itemList.get(newItemPosition)) == 0;
            }

            @Override
            public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                return sameContentComparator.compare(mItems.get(oldItemPosition),
                        itemList.get(newItemPosition)) == 0;
            }
        });

        mItems.clear();
        mItems.addAll(itemList);

        result.dispatchUpdatesTo(new ListUpdateCallback() {

            @Override
            public void onInserted(int position, int count) {
                if (DEBUG) {
                    Log.e(TAG, "onInserted: ");
                }
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                if (DEBUG) {
                    Log.e(TAG, "onRemoed: ");
                }
                notifyItemRangeRemoved(position, count);
            }

            /**
             * Currently Leanback support library doesn't support move operation, use item range
             * changed method to replace it as a temporal solution.
             * @param fromPosition from position.
             * @param toPosition to position.
             */
            @Override
            public void onMoved(int fromPosition, int toPosition) {
                if (DEBUG){
                    Log.e(TAG, "onMoved: ");
                }
                notifyItemRangeChanged(fromPosition, toPosition - fromPosition + 1);
            }

            @Override
            public void onChanged(int position, int count, Object payload) {
                if (DEBUG) {
                    Log.e(TAG, "onChanged: ");
                }

                // the support for payload has not been added to leanback support library, just
                // ignore it currently.
                notifyItemRangeChanged(position, count);
            }
        });
    }