Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecylcerView scrolls to Top on Item Removed

I use the following Code in my RecyclerView.Adapter to remove an Item. If the current scroll-position is at the top, everything works fine.

If the scroll-position is somewhere in the middle of the List, the List automatically scrolls to the Top of the View.

How to avoid this behaviour?

public class MyAdapter extends RecyclerView.Adapter {

...

    public void removeItem(int pos){
        mDataset.remove(pos); //List with Items
        notifyItemRemoved(pos);
        notifyItemRangeChanged(pos,mDataset.size());
    }
}

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

    </LinearLayout>
...
    </FrameLayout>
like image 551
Marcel Avatar asked Oct 31 '22 06:10

Marcel


1 Answers

Adding the following Line solved it for me

recyclerView.setHasFixedSize(true);
like image 199
Marcel Avatar answered Nov 09 '22 15:11

Marcel