animateLayoutChanges does not work well with nested layout?

前端 未结 5 770
既然无缘
既然无缘 2020-12-07 18:57

I have a nested layout like the following:

      
        
              


        
5条回答
  •  无人及你
    2020-12-07 19:18

    Ok, after digesting the first answer, I make it simple here, for those who don't get proper animation result when using android:animateLayoutChanges="true" in NESTED layout:

    1. Make sure you add android:animateLayoutChanges="true" to the will-be-resized ViewGroup (LinearLayout/RelativeLayout/FrameLayout/CoordinatorLayout).

    2. Use setVisibility() to control the visibility of your target View.

    3. Listen carefully from here, add android:animateLayoutChanges="true" to the outer ViewGroup of your will-be-resized ViewGroup, this outer ViewGroup must be the one who wraps all the position-changing View affected by the animation.

    4. Add following code in your Activity before the setVisibility(), here the rootLinearLayout is the outer ViewGroup I mentioned above:

       LayoutTransition layoutTransition = rootLinearLayout.getLayoutTransition();
       layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
      

    Before:

    After:


    Reminder: If you miss the 3rd step, you will get null pointer exception.

    Good luck!

提交回复
热议问题