【问题标题】:Exclude certain elements from being animated in android layout transitions在 android 布局转换中排除某些元素的动画
【发布时间】:2021-09-17 22:51:06
【问题描述】:

我有一个关于 android 布局转换框架的问题。特别是我想实现一种效果,即布局的某个部分根据另一个视图的可见性向下或向上滑动。

想象一下下面的布局。 (请忽略这里的嵌套线性布局;))

<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>

现在我想要实现的是,当 changedView1changedView2 改变它们的可见性时,movingView 会向上或向下滑动。

通过为父布局启用 LayoutTransition.CHANGING,滑动部分可以正常工作。但这有一个副作用,即 movingView 也会在添加或删除项目时进行动画处理,因为此布局更改了其边界。这就是我的问题,因为这会导致看起来非常奇怪的动画。

回到我的问题。有没有办法在 movingView 上保持滑动动画而不为布局绑定更改设置动画?

movingView 上禁用 layoutTransitions 显然没有帮助,因为这只会影响子视图的动画。我还尝试在父布局上启用和禁用不同的 LayoutTransitions,但到目前为止还没有达到预期的效果。

如果我可以添加更多详细信息,请告诉我,否则我希望有人可以帮助我。

提前致谢!

【问题讨论】:

    标签: android android-animation layouttransition


    【解决方案1】:

    我知道它已经晚了,但希望它可以帮助某人。由于android:animateLayoutChanges 是直接父级的属性,因此您可以将View/Layout 包装在FrameLayout(或其他一些ViewGroup)中,并在新父级上设置android:animateLayoutChanges="false"

    【讨论】:

      【解决方案2】:

      为避免不需要的动画,您可以在需要时通过代码删除动画布局更改,如下所示:

      //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());});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多