Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView remove item animation bug

I'm trying to implement a ToDo list that contains a list and some other views below it on the activity page.

I am using a LinearLayout for the entire page and a RecyclerView for the list, alongside other views below the RecyclerView (ImageView, Buttons...etc.)

Here's my final view hierarchy:

<LinearLayout>
    <TextView />
    <RecyclerView />
    <Button />
    <EditText />
    <ImageView />
</LinearLayout>

I have implemented the RecyclerView where I can add and remove items. I am using a LinearLayoutManager without specifying an ItemAnimator so DefaultItemAnimator is used.

Adding items to the list works as expected. My problem is that the page doesn't animate well when I remove an item from the RecyclerView (by removing it from the dataset first then using RecyclerViewAdapter.notifyItemRemoved).

What happens is that the entire page snaps first to adapt to the new RecyclerView height, and then the RecyclerView item remove animation completes, which makes the behavior of the page look weird since all the views below the RecyclerView snap up while the deleted item fades out but hasn't lost its height yet, then the remaining RecyclerView items (below the deleted item) scroll up, looking like they're sliding up from under a wall.

I tried to look for solutions on the web and couldn't find anything to solve my problem.

I have found this unanswered question describing the same problem. Please refer to it in case my explanation wasn't clear enough.

Anyone encountering the same issue? Any suggestions?

Thanks.

like image 574
Copy33 Avatar asked Mar 08 '17 06:03

Copy33


People also ask

How do I stop RecyclerView animation?

How to remove scroll effect from RecyclerView android? You can open or close it. Basically, you need to add android:overScrollMode=“never” line to your RecyclerView.

Why does a RecyclerView need an adapter?

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView .


1 Answers

I had the same problem. In my situation just this line helped:

recyclerView.setHasFixedSize(true);
like image 178
Mehmed Mert Avatar answered Sep 21 '22 03:09

Mehmed Mert