android - MotionLayout 多个 Transitions

标签 android android-motionlayout

首先,我使用的是 androidx.constraintlayout:constraintlayout:2.0.0-beta1,因为 beta2 在我们的应用中打乱了很多布局。

我试图让 MotionLayout 在不同情况下播放两种不同的过渡:

1) 当点击某个项目时,我想让它展开和折叠; 2) 滚动时,想法是让另一个项目折叠和展开(类似于折叠工具栏)。

经过一些研究,我发现 MotionLayout 不会在其 layoutDescription 中接受多个 Transition 标签(它只会加载它能找到的第一个标签)。所以,为了让我的实现工作,我这样做了:

在我的 MotionScene 中,我只有滚动的 Transition 元素及其两个 ConstraintSet。我还为点击动画定义了必要的 ConstraintSets。

<Transition
    android:id="@+id/scroll_transition"
    app:constraintSetStart="@id/notifications_expanded"
    app:constraintSetEnd="@id/notifications_collapsed">

    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@id/swiperefresh_layout"
        app:touchAnchorSide="top"
        app:moveWhenScrollAtTop="true"/>

</Transition>

<ConstraintSet android:id="@+id/notifications_expanded">
    <Constraint android:id="@id/notifications"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header_info_container"
        app:layout_constraintEnd_toEndOf="parent"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/notifications_collapsed">
    <Constraint android:id="@id/notifications"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/header_info_container"
        app:layout_constraintEnd_toEndOf="parent"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/header_collapsed">

    <Constraint android:id="@id/header_info_container"
        android:visibility="gone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header_layout"
        app:layout_constraintEnd_toEndOf="parent"/>

</ConstraintSet>

<ConstraintSet android:id="@+id/header_expanded">

    <Constraint android:id="@id/header_info_container"
        android:visibility="visible"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header_layout"
        app:layout_constraintEnd_toEndOf="parent"/>

</ConstraintSet>

在布局的 xml 中,我设置了 layoutDescription;

<androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motion_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/transitions">

滚动过渡效果如预期般完美。为了让点击动画真正起作用,我手动调用它:

if (headerInfoContainer.getVisibility() == View.VISIBLE) {
    motionLayout.setTransition(R.id.header_expanded, R.id.header_collapsed);
    motionLayout.setTransitionDuration(300);
    motionLayout.transitionToEnd();
} else {
    motionLayout.setTransition(R.id.header_collapsed, R.id.header_expanded);
    motionLayout.setTransitionDuration(300);
    motionLayout.transitionToEnd();
}

但是,在该 fragment 之后,滚动停止工作。我怀疑它已被禁用,但我找不到如何将其恢复原状。缺乏文档也于事无补。

提前致谢!

最佳答案

我知道这是一篇旧帖子,但我会尽力为其他可能有同样问题的人解答。 实际上,是的,您可以在您的 MotionScene 中有多个 Transition,但它们一次只能工作一个。 我可能不完全理解你需要什么,但如果你想要其他 View ,在你的标题中,在你的标题折叠/展开时设置动画,你需要在你的 ConstraintSet 中设置它们的 Constraint 属性。 请记住,ConstraintSet 就像您的 View 的一种状态,它从一种状态变为另一种状态,因此对于需要在特定集合上更改的每个 View ,您需要使用 ConstraintSet 设置新 View 的属性。 像这样的东西:

   <Transition
    android:id="@+id/scroll_transition"
    app:constraintSetStart="@id/notifications_expanded"
    app:constraintSetEnd="@id/notifications_collapsed">

    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@id/swiperefresh_layout"
        app:touchAnchorSide="top"
        app:moveWhenScrollAtTop="true"/>

</Transition>

<ConstraintSet android:id="@+id/notifications_expanded">
    ... How your views should change from default layout when getting on this state
</ConstraintSet>

<ConstraintSet android:id="@+id/notifications_collapsed">
    ... How your views should change from default layout when getting on this state
</ConstraintSet>

在您的 <Transition> 上标记您告诉 MotionScene 什么是开始和结束状态。

关于android - MotionLayout 多个 Transitions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236774/

相关文章:

java - 在 Android 中,有没有使用类来格式化一组 View 的方法?

android - 是否可以使用 MotionLayout 为组的可见性设置动画?

java - 是否可以使用Java/Kotlin在Android MotionLayout中从“开始”状态切换到“结束”状态?

android - 如何让 NestedScrollView 在 MotionLayout 中滚动?

java - 按钮在第二次点击android上工作

android - 在连接的任何(空)NFC 上启动应用程序

gradle - 添加constraintLayout后无法同步项目

android-studio - 运动布局 : How to change view visibility on last frame

android - 任务 ':compileDebugJavaWithJavac'的执行失败。 >编译失败;有关详细信息,请参见编译器错误输出。 ionic 的

android - 使用 javacard 生成签名 CSR 时出错(asn1 错误标记值)