Animating layouts using the "animateLayoutChanges" attribute in XML

I have a LinearLayout for which I am using android:animateLayoutChanges="true" in the parent LinearLayout . When the user presses the toggle button, LinearLayout "collapses" (I set the view visibility as LinearLayout.GONE programmatically, and when I click on it again, it expands by setting the visibility to LinearLayout.VISIBLE .

Animation of its folding and expansion works correctly.

However, all elements under the resettable / expandable LinearLayout anchored to the vertex until the convolution animation is complete. Elements that are snapped back are NOT inside the parent whose animateLayoutChanges set to true , and I don’t think I can put it inside it.

Here is my layout hierarchy (I didn't mention attributes to keep it short):

 <!-- Top most LinearLayout--> <LinearLayout> <!-- LinearLayout containing android:animateLayoutChanges="true"--> <LinearLayout> <!-- RelativeLayout containing button to toggle LinearLayout visibility below--> <RelativeLayout> </RelativeLayout> <!-- LinearLayout that has its visibility toggled --> <LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> 

All this layout is inserted programmatically into another LinearLayout (see below):

 <LinearLayout android:id="@+id/form_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- This is where the previous XML layout containing the toggle-able LinearLayout is inserted programmatically. --> <!-- This button snaps back up to the top before the animation is complete. --> <Button /> </LinearLayout> 

I understand that the problem will be solved if I add a Button that binds to a LinearLayout that has animateLayoutChanges as true. However, this is not an option for several reasons.

So is there any other job?

+7
android android-layout xml android-animation
source share
2 answers

Instead of using animateLayoutChanges try adding TransitionManager.beginDelayedTransition(transitionsContainer); in the onClick method, where transitionsContainer is the parent of the views to be animated.

For example, your parent layout is

 <LinearLayout android:id="@+id/transitions_container"> <!--add your widgets here--> </LinearLayout> 

And in code

 final ViewGroup transitionsContainer = (ViewGroup) view.findViewById(R.id.transitions_container); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TransitionManager.beginDelayedTransition(transitionsContainer); // do your staff with changing children of transitionsContainer } }); 

Read more about https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50 .

+1
source share

Just a thought ... What if you remove the "animateLayoutChanges" from the inline layout and add it to the parent layout (second XML)? I suspect this will all come to life. You may need to set the true property in the code as you programmatically insert the layout.

Here's how to do it programmatically Stack Overflow

Another option would be to use the .animate property programmatically on a button that casts back

 myButton.animate().translationY(floatYposition).setDuration(300); //300 milliseconds 
0
source share

All Articles