How do I chain together layout change animations with the LayoutTransition class or otherwise?

Viewed 10

I have a ViewGroup on which I have set the android:animateLayoutChanges property to true.

As a result, when I toggle the visibility of one of the Views in the ViewGroup between View.VISIBLE and View.GONE, I get a nice layout change animation as follows:

Animated layout changes

I'd like to take this idea a step further and chain together a sequence of transitions. For example, hide "Two" and then show "Three".

How do I do this?

1 Answers

I have managed to achieve this as follows:

  1. Define a LayoutTransition.TransitionListener which sets the visibility of the View that I want to show as View.VISIBLE when the endTransition(...) method is called.
  2. Get the LayoutTransition object in the ViewGroup and add the LayoutTransition.TransitionListener defined in step 1 to it.
  3. Set the visibility of the View that I want to hide as View.GONE.

This achieves a sequence of animations as follows:

Animated layout changes

This works but seems a little fragile and is a somewhat clumsy solution for scenarios where I need to add a third or fourth animation to the sequence.

If anybody has suggestions for a simpler means of chaining together layout change animations, I'm open to it.

Notes

  1. My description of the implementation above is a little simplified and omits some work I'm doing to remove the LayoutTransition.TransitionListener when a given transition ends. For the concrete details, see here.
Related