How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged

前端 未结 8 1122
有刺的猬
有刺的猬 2020-12-13 03:20

In my project I need disable the \"change\" animation of RecyclerView while notifyItemChanged.

I investigated in the source of Recycl

相关标签:
8条回答
  • 2020-12-13 03:53

    Nothing of the above solutions worked for me. What worked was using the TransitionManager and setting the scene transition to null

    TransitionManager.go(new Scene(recyclerView), null);
    

    To re-enable the default animation you can create a new AutoTransition and set it to the same method above:

    AutoTransition autoTransition = new AutoTransition();
    TransitionManager.go(new Scene(recyclerView), autoTransition);
    
    0 讨论(0)
  • 2020-12-13 03:57

    I've created an Extention Class, to disable animation

    import androidx.recyclerview.widget.RecyclerView
    import androidx.recyclerview.widget.SimpleItemAnimator
    
    fun RecyclerView.disableAnimation() {
        (itemAnimator as? SimpleItemAnimator)?.supportsChangeAnimations = false
    }
    

    You can call it like this:

    recyclerView.disableAnimation()
    
    0 讨论(0)
  • 2020-12-13 04:06

    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:

    recyclerView.setItemAnimator(new MyItemAnimator());
    

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

    animateChangeImpl(final ChangeInfo changeInfo) { ... }
    

    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)

    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() { ... }
    
    0 讨论(0)
  • 2020-12-13 04:07

    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

    recyclerView.setItemAnimator(null);
    

    on the RecyclerView.

    0 讨论(0)
  • 2020-12-13 04:07

    Just if someone stumbles like me:
    Somehow setSupportsChangeAnimations(false) didn't work for me, but recyclerView.getItemAnimator().setChangeDuration(0) has just removed the animation nicely.

    0 讨论(0)
  • 2020-12-13 04:14

    The easiest solution is to extend DefaultItemAnimator and set setSupportsChangeAnimations to false right in the constructor:

    public class DefaultItemAnimatorNoChange extends DefaultItemAnimator {
        public DefaultItemAnimatorNoChange() {
            setSupportsChangeAnimations(false);
        }
    }
    
    0 讨论(0)
提交回复
热议问题