View Expand Animation Affects Other Views by Using "animateLayoutChanges" in Nested Layout

474 Views Asked by At

For the orange part, I use android:animateLayoutChanges="true" on its LinearLayout with setVisibility(visible/gone) to achieve the expand animation, but the last TextView (the whole row) looks affected by that animation (it flickrs), also the whole part below the orange part didn't move up/down smoothly (looks no animation at all). Any help I will be appreciated. Thank you.

enter image description here

1

There are 1 best solutions below

1
Sam Chen On BEST ANSWER

Ok, finally I found the solution:

  1. Add android:animateLayoutChanges="true" to your will-be-resized ViewGroup.

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

  3. Add android:animateLayoutChanges="true" to the outer ViewGroup of your will-be-resized ViewGroup. This outer ViewGroup must be the one who contains all the position-will-be-changed View.

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

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

Reminder:

  1. If you miss the 3rd step, you will get a null pointer exception.
  2. If you miss the 1st step, you will still get a good expand animation, but a bad collapse, you will see your target View will be disappeared suddenly first, then the ViewGroup collapses.

Result:

enter image description here