Marcel
Marcel

Reputation: 162

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>

Upvotes: 5

Views: 3330

Answers (7)

Aleksey Kislyakov
Aleksey Kislyakov

Reputation: 64

I use recyclerView inside nestedScrollView and had the same issue. Here is the code to call to remove an item

    mDataset.remove(pos) // Remove from dataset
    notifyItemRemoved(pos) // Show animation of removing

And here are lines to be added to XML to make it scroll correctly (RecyclerView is focused with higher priority than its viewHolders so it won't scroll)

    android:descendantFocusability="beforeDescendants"
    android:nestedScrollingEnabled="false"

After that everything was ok.

Upvotes: -1

erluxman
erluxman

Reputation: 19415

Try this code:

public void removeItem(int position){

    mdata.remove(position);
    notifyItemRangeChanged(position,mdata.size());
}

Upvotes: 0

Marcel
Marcel

Reputation: 162

Adding the following Line solved it for me

recyclerView.setHasFixedSize(true);

Upvotes: 1

am110787
am110787

Reputation: 316

Try this

  1. mDataset.remove(pos);
  2. adapter.notifyItemRemoved(pos);
  3. adapter.notifyDataSetChanged();

This should work.

Upvotes: 0

Reaz Murshed
Reaz Murshed

Reputation: 24231

You need to scroll to the position removed after notifying the dataset associated with the RecyclerView is changed.

And yes, you just have to call notifyDataSetChanged() after removing an item from the mDataset. So your adapter will look like this.

public class MyAdapter extends RecyclerView.Adapter {

    //... Code goes here 

    public void removeItem(int pos){
        mDataset.remove(pos); 
        notifyDataSetChanged();
        recyclerView.scrollToPosition(pos - 1);
    }
}

Upvotes: 0

Ant&#243;nio Paulo
Ant&#243;nio Paulo

Reputation: 351

Use this :

mDataset.remove(pos);
adapter.notifyItemRemoved(pos);
adapter.notifyDataSetChanged();

Upvotes: 0

peeyush pathak
peeyush pathak

Reputation: 3663

for recycler view you use yourRecylerView.scrollToPosition(position);

Just check whether this position is present in the list.

Upvotes: 0

Related Questions