Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent auto scroll in RecyclerView after notifyDataSetChanged?

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

  • 07/21/2017
  • Message 1
  • Message 2
  • Message 3
  • 07/22/2017
  • Message 4
  • New messages header
  • Message 5
  • Message 6

When new messages arrived I sort it by date and call notifyDataSetChanged(). It works well, but it automatically scroll my RecyclerView on new item(s) height. In this case I cannot use notifyItemInserted() because I don't know item position after sorting.

How to force RecyclerView do not scroll after notifyDataSetChanged() without call notifyItemInserted()?

like image 835
BArtWell Avatar asked Jul 26 '17 11:07

BArtWell


People also ask

How does notifydatasetchanged work in recyclerview?

When we will call the notifyDataSetChanged () method it will not handle the complete reordering of the RecyclerView adapter but it will find if the item at that position is the same as before to do less work.

Why does recyclerview scroll to the bottom when data is loaded?

RecyclerView automatically scrolls to the bottom when data is loaded the first time because of call to notifyItemInserted (index). · Issue #886 · firebase/FirebaseUI-Android · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

How does recyclerview work in Android?

RecyclerView, as its name suggests, recycles views once they get out of scope (screen) with the help of ViewHolder pattern. So how does this affect your app? Just like I wrote up there, RecyclerView recycles the views once they get out of the screen. While trying to reuse the cache view, binding with the new data sometimes goes wrong.

Why does recyclerview save the view state?

It is because RecyclerView recycles every view which is not visible to the user. So, when the user scrolls back it recycles the previous view which is in normal form. So, today I’m going to explain how we can save the view state. The following shows the model we’re going to use in this example.


2 Answers

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());
like image 131
nhp Avatar answered Sep 24 '22 18:09

nhp


Have you played with myRecView.getLayoutManager().setStackFromEnd(boolean) ?

If you can determine if the new item has been inserted after sorting above or below the visible part and in function of this call setStackFromEnd () with true or false maybe you can avoid the scroll.

I do not know if it will really work ....

like image 41
from56 Avatar answered Sep 26 '22 18:09

from56