在多状态过渡中,只有第一个过渡才有效

问题描述

我正在测试多状态转换,就像下面的示例一样。

https://github.com/googlearchive/android-ConstraintLayoutExamples/blob/multi_state/motionlayout/src/main/res/xml/scene_26.xml

我的布局非常简单。它下面有一个带有recyclerview的标题。因此,出于测试目的,我尝试执行以下转换。

转换1 标头的大小从500dp(开始状态)更改为200dp(中间状态)

过渡2 标头的大小从200dp(中间状态)更改为0dp(结束状态)

问题是仅发生第一次转换。我想要的行为是,当我滚动列表T1时发生,而紧随其后的T2应该出现,使标题完全折叠。

请注意-我这样做仅供测试。我需要开发一个动画,在这里使用相同的原理

这是代码

布局

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motion_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:showPaths="true"
        app:layoutDescription="@xml/scrollable_header_above_recycler_view_scene">

        <ImageView
            android:id="@+id/header"
            android:layout_width="0dp"
            android:layout_height="500dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:paddingTopSystemWindowInsets="@{true}"
            tools:ignore="ContentDescription" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:clipToPadding="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/header"
            app:paddingBottomSystemWindowInsets="@{true}" />

    </androidx.constraintlayout.motion.widget.MotionLayout>
</layout> 

MotionLayout

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/intermediate"
      >
        <OnSwipe
            motion:onTouchUp="stop"
            motion:dragDirection="dragUp"
            motion:touchAnchorSide="bottom"
            motion:touchAnchorId="@+id/header" />
    </Transition>

    <Transition
        motion:constraintSetStart="@+id/intermediate"
        motion:constraintSetEnd="@+id/end"
        >
        <OnSwipe
            motion:onTouchUp="stop"
            motion:dragDirection="dragDown"
            motion:touchAnchorSide="bottom"
            motion:touchAnchorId="@+id/header" />
    </Transition>





    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />


    </ConstraintSet>


    <ConstraintSet android:id="@+id/intermediate">
        <Constraint
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            />
    </ConstraintSet>


    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            />
    </ConstraintSet>


</MotionScene> 

解决方法

看起来像个错误。我已经将其报告给了Google问题跟踪器。...您可以使用侦听器手动设置过渡和进度,以防止出现这种情况,例如:

// MotionLayout
motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
        override fun onTransitionTrigger(p0: MotionLayout?,p1: Int,p2: Boolean,p3: Float) {}
        override fun onTransitionStarted(p0: MotionLayout?,p2: Int) {}
        override fun onTransitionChange(p0: MotionLayout?,p2: Int,p3: Float) {}
        override fun onTransitionCompleted(p0: MotionLayout?,p1: Int) {
            if (motionLayout.currentState == R.id.set1 && motionLayout.endState == R.id.set1) {
                motionLayout.progress = 0F
                motionLayout.setTransition(R.id.set1,R.id.set2)
            } else if (motionLayout.currentState == R.id.set1 && motionLayout.endState == R.id.set2) {
                motionLayout.progress = 1F
                motionLayout.setTransition(R.id.set0,R.id.set1)
            }
        }
})
,

MotionLayout不支持NestedScrollView / RecyclerView的多状态转换。 它将为许多极端情况提供方法。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...