Exclude certain elements from being animated in android layout transitions
Asked Answered
W

2

12

I have a question regarding the android layout transition framework. In particular i want to achieve an effect that a certain part of an layout slides down or up depending on the visibility of another view(s).

Imagine the following layout. (And please overlook the nested LinearLayouts here ;) )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <View
                android:id="@+id/changingView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

        <View
                android:id="@+id/changingView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

    </LinearLayout>

    <LinearLayout
            android:id="@+id/movingView"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="false"/>

</LinearLayout>

Now what i want to achieve is that when changingView1 and changingView2 change their visibility that movingView slides up or down.

By enabling the LayoutTransition.CHANGING for the parent layout the sliding part works fine so. But this has the side effect that the movingView will also be animated when there are being items added or removed because this layout changes its bounds. And here lies my problem because this results in a very strange looking animation.

So back to my question. Is there a way to keep the sliding animation without animating layout bound changes on the movingView?

Disabling layoutTransitions on the movingView obviously does not help because this only effects the animations of the child views. I also tried playing around with enabling and disabling different LayoutTransitions on the parent layout but so far without the desired effect.

If i can add more details please let me know otherwise i hope someone can help me out.

Thanks in advance!

Waggoner answered 18/8, 2016 at 9:16 Comment(0)
L
6

I know it's late but hope it can help someone. Since android:animateLayoutChanges is the property of the direct parent, you can wrap your View/Layout in a FrameLayout (or some other ViewGroup) and set android:animateLayoutChanges="false" on the new parent.

Lang answered 9/5, 2018 at 7:12 Comment(0)
F
1

To avoid unwanted animations you can remove the animate layout changes by code when needed, something like this:

//removing the animate layout changes to prevent the default animation for the newly added items
parentLayout.setLayoutTransition(null);

/* do some logic to add the new views */

//add the animate layout changes back so the over changes will be still animated
new Handler().post(() -> {parentLayout.setLayoutTransition(new LayoutTransition());});
Finally answered 20/3, 2020 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.