Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override animation for notifyItemChanged in RecyclerView.Adapter

Well, I have a RecyclerView with an adapter and everything works great. The items in the ArrayList dataset are being updated periodically. So the items and their elements as well as their position in the list change. This is achieved by simple sorting and manually calling these methods, whenever things happen:

// swapping two items
Collections.swap(items, i, j);
itemsAdapter.notifyItemMoved(i, j);

// adding a new one
itemAdapter.notifyItemInserted(items.size());

// when updating valus
itemAdapter.notifyItemChanged(i);

The latter of which, is the cause of my misery. Every time an item is updated, a little "blink" animation is triggered.

I found a couple of solutions for this:

// disabling all animations
recyclerView.getItemAnimator().setSupportsChangeAnimations(false);

// or

// setting the animation duration to zero,
recyclerView.getItemAnimator().setChangeDuration(0);

But both of these kill the animations when items move (being swapped). I just want to override the one animation and keep all of this magic. Is there a way of doing this? And if it's overriding ItemAnimator, does anyone have a simple example?

Thanks in advance!

like image 625
Alex Avatar asked Aug 08 '15 18:08

Alex


2 Answers

There is a dedicated method to disable just item changed animations:

((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

Official docs.

like image 95
Pablo A. Martínez Avatar answered Nov 12 '22 16:11

Pablo A. Martínez


Yes, I did.

First, get the source code of DefaultItemAnimator. Take the code and create a class named MyItemAnimator in your project. Then, set the ItemAnimator to a new instance of your modified MyItemAnimator, like so:

recyclerView.setItemAnimator(new MyItemAnimator());

Now, go in the new classes source code and locate the method

animateChangeImpl(final ChangeInfo changeInfo) { ... }

We simply have to locate the method calls changing alpha values. Find the following two lines and remove the .alpha(0) and .alpha(1)

oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { ... }
newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()).alpha(1).setListener(new VpaListenerAdapter() { ... }

like so

oldViewAnim.setListener(new VpaListenerAdapter() { ... }
newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()).setListener(new VpaListenerAdapter() { ... }
like image 24
Alex Avatar answered Nov 12 '22 15:11

Alex