In my project I need disable the "change" animation of RecyclerView
while notifyItemChanged
.
I investigated in the source of RecyclerView
and had overridden android.support.v7.widget.DefaultItemAnimator
as below:
private static class ItemAnimator extends DefaultItemAnimator
{
@Override
public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
if(oldHolder != null)
{
oldHolder.itemView.setVisibility(View.INVISIBLE);
dispatchChangeFinished(oldHolder, true);
}
if(newHolder != null)
{
dispatchChangeFinished(newHolder, false);
}
return false;
}
}
But I am not sure if I match the spec of the Google document:
RecyclerView.ItemAnimator.animateChange
According to my understanding source code, if I do not override the method properly, the oldHolder will not be recycled.
Please help me figure out how to override animateChange
in a correct way.
I have found the correct solution to just remove the animateChange.
It's very simple. Google has implemented the functionality.
Documentation: setSupportsChangeAnimations
Just if someone stumbles like me:
Somehow
setSupportsChangeAnimations(false)
didn't work for me, butrecyclerView.getItemAnimator().setChangeDuration(0)
has just removed the animation nicely.I had the same problem. When calling notifyItemChanged there was a red overlay flashing. After experimenting around with your code I finally removed the default Animator by simply calling
on the RecyclerView.
@Kenny answer didn't work anymore because google remove method
setSupportsChangeAnimations()
(but why?) in support library 23.1.0.In some case
setChangeDuration(0)
can work as workaround.@edit I suggest use something like that:
If found a solution, for everyone who wants to keep all the animations given by the DefaultItemAnimator, but getting rid of the "blink" animation that occurs every time the view is updated.
First, get the source code of DefaultItemAnimator. Create a class with the same name in your project.
Second, set the ItemAnimator to a new instance of your modified DefaultItemAnimator, like so:
Then, go in the new classes source code and locate the method
Now, we simply have to locate the method calls changing alpha values. Find the following two lines and remove the .alpha(0) and .alpha(1)
like so
The easiest solution is to extend
DefaultItemAnimator
and setsetSupportsChangeAnimations
tofalse
right in the constructor: