How to prevent auto scroll in RecyclerView after notifyDataSetChanged?

前端 未结 3 370
Happy的楠姐
Happy的楠姐 2021-01-14 10:31

I have a RecyclerView with several items types. It looks like this:

  • 07/21/2017
  • Message 1
  • Message 2
  • Message 3
  • 0
3条回答
  •  独厮守ぢ
    2021-01-14 11:08

    There are 4 ways you can try. Because I don't know what is the height of your view, so I can't sure if it really works. But let give it a try

    I. You don't which specific item could be changed. But you know the range of that change

    mAdapter.notifyItemInserted(position);                 
    mAdapter.notifyItemRangeChanged(0, list.size());
    

    II. Clear list before call notifyDataSetChanged()

    yourList.clear();
    mAdapter.notifyDataSetChanged();
    

    III. Save/Reset state of ReyclerView before/after adding new item

        // Save state
        private Parcelable recyclerViewState;
        recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
    
        // Restore state
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
    

    IV. Use this function

    mAdapter.notifyItemRangeChanged(0, list.size());
    

提交回复
热议问题