Diffutil in recycleview, making it autoscroll if a new item is added

后端 未结 3 1082
栀梦
栀梦 2021-01-01 14:00

If we use DiffUtil.Callback, and do

adapter.setItems(itemList);
diff.dispatchUpdatesTo(adapter);

how can we make sure that ad

3条回答
  •  甜味超标
    2021-01-01 14:05

    You have a dispatchUpdatesTo(ListUpdateCallback) method to use as well.

    So you could just implement a ListUpdateCallback which gives you the first element inserted

    class MyCallback implements ListUpdateCallback {
        int firstInsert = -1;
        Adapter adapter = null;
        void bind(Adapter adapter) {
            this.adapter = adapter;
        }
        public void onChanged(int position, int count, Object payload) {
            adapter.notifyItemRangeChanged(position, count, payload);
        }
        public void onInserted(int position, int count) {
            if (firstInsert == -1 || firstInsert > position) {
                firstInsert = position;
            }
            adapter.notifyItemRangeInserted(position, count);
        }
        public void onMoved(int fromPosition, int toPosition) {
            adapter.notifyItemMoved(fromPosition, toPosition);
        }
        public void onRemoved(int position, int count) {
            adapter.notifyItemRangeRemoved(position, count);
        }
    }
    

    and then just scroll the RecyclerView manually

    myCallback.bind(adapter)
    adapter.setItems(itemList);
    diff.dispatchUpdatesTo(myCallback);
    recycler.smoothScrollToPosition(myCallback.firstInsert);
    

提交回复
热议问题