MotionLayout - 单击布局时行为不正确

MotionLayout - clicking on the layout behaves incorrectly

对于我正在使用 MotionLayout 的项目,除了一件小事外,一切都运行良好且顺利。当我打开应用程序并按下屏幕上的某个位置时,有时布局会跳转到未定义状态。

我定义的状态是: - 应用以 'broken-white' 背景开始 - 向右滑动时,会显示蓝色背景的框架布局 - 向上滑动时,将显示红色背景的框架布局,隐藏所有其他 imageViews

按下屏幕某处时应用程序所处的状态 - 出现红色背景,所有 imageView 都可见。

我一直在寻找发生这种情况的原因,我猜这是一个错误(因为它仍处于 alpha 阶段),但想听听别人的意见。

将附上代码以相当容易地重现它。

主要布局:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
        android:id="@+id/fl_forUnity"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:layout_height="match_parent"
        android:alpha="1"
        tools:ignore="UnusedAttribute"/>

<androidx.constraintlayout.motion.widget.MotionLayout
        app:layoutDescription="@xml/scene_motion"
        android:id="@+id/topMotionLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <View
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <View
            android:id="@+id/ivBottomAnchor"
            android:layout_width="match_parent"
            android:layout_height="1dp"/>

    <FrameLayout
            android:id="@+id/flBottom"
            app:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <View
            android:id="@+id/ivLeftAnchor"
            android:layout_width="1dp"
            android:layout_height="match_parent"/>

    <FrameLayout
            android:id="@+id/flLeft"
            app:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
            android:layout_width="300dp"
            android:layout_height="match_parent"/>

    <View
            android:id="@+id/ivRightAnchor"
            android:layout_width="1dp"
            android:layout_height="match_parent"/>

    <FrameLayout
            android:id="@+id/flRight"
            app:layout_constraintStart_toEndOf="@id/ivRightAnchor"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="0dp"
            android:layout_height="match_parent"/>

    <ImageView
            android:layout_width="32dp"
            android:src="@drawable/ic_message"
            android:layout_height="32dp"
            tools:tint="@color/colorGreen_200"
            android:id="@+id/fabStartButton"/>

    <ImageView
            android:id="@+id/middleButton"
            tools:tint="@color/colorGreen_200"
            android:layout_width="64dp"
            android:src="@drawable/ic_circle"
            android:layout_height="64dp"/>

    <ImageView
            android:id="@+id/bottomButton"
            tools:tint="@color/colorGreen_200"
            android:layout_width="28dp"
            android:src="@drawable/ic_message"
            android:layout_height="28dp"/>

    <ImageView
            android:layout_width="32dp"
            android:layout_height="32dp"
            tools:tint="@color/colorGreen_200"
            android:src="@drawable/ic_adb"
            android:id="@+id/fabEndButton"/>

    <ImageView
            android:src="@drawable/ic_orb"
            android:id="@+id/ivOrb"
            tools:tint="@color/colorGreen_200"
            android:layout_width="6dp"
            android:layout_height="6dp"/>

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

动态布局:

<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

<Transition
        android:id="@+id/SwipeUp"
        motion:constraintSetStart="@+id/beginning"
        motion:constraintSetEnd="@+id/endAtTop">
    <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/ivBottomAnchor"
            motion:touchAnchorSide="top"/>

    <KeyFrameSet>
        <KeyAttribute
                android:alpha="0"
                motion:framePosition="15"
                motion:target="@id/bottomButton"/>

        <KeyAttribute
                android:alpha="0"
                motion:framePosition="15"
                motion:target="@id/fabStartButton"/>

        <KeyAttribute
                android:alpha="0"
                motion:framePosition="15"
                motion:target="@id/middleButton"/>

        <KeyAttribute
                android:alpha="0"
                motion:framePosition="15"
                motion:target="@id/fabEndButton"/>

        <KeyAttribute
                motion:target="@id/backgroundView">
            <CustomAttribute
                    motion:attributeName="backgroundColor"
                    motion:customColorValue="#B71C1C"/>
        </KeyAttribute>
    </KeyFrameSet>
</Transition>

<Transition
        android:id="@+id/SwipeLeft"
        motion:constraintSetStart="@+id/beginning"
        motion:constraintSetEnd="@+id/endRighty">

    <OnSwipe
            motion:dragDirection="dragLeft"
            motion:touchAnchorId="@id/ivRightAnchor"
            motion:touchAnchorSide="top"/>
</Transition>

<Transition
        android:id="@+id/SwipeRight"
        motion:constraintSetStart="@+id/beginning"
        motion:constraintSetEnd="@+id/endLefty">

    <OnSwipe
            motion:dragDirection="dragRight"
            motion:touchAnchorId="@id/ivLeftAnchor"
            motion:touchAnchorSide="top"/>

    <KeyFrameSet>
        <KeyAttribute
                motion:target="@id/backgroundView">
            <CustomAttribute
                    motion:attributeName="backgroundColor"
                    motion:customColorValue="#0D47A1"/>
        </KeyAttribute>
    </KeyFrameSet>
</Transition>

<!--TODO: pressing on the bottombutton, go to bottom screen-->
<ConstraintSet android:id="@+id/beginning">
    <Constraint
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0"/>

    <Constraint
            android:id="@+id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>

    <Constraint
            android:id="@+id/ivBottomAnchor"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_height="1dp"
            android:layout_width="match_parent"/>

    <Constraint
            android:id="@+id/flBottom"
            motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivLeftAnchor"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flLeft"
            motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
            android:layout_width="300dp"
            android:layout_marginEnd="10dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flRight"
            android:layout_width="0dp"
            motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/fabStartButton"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_marginBottom="32dp"
            android:layout_marginStart="32dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/middleButton"
            android:layout_width="78dp"
            android:layout_height="78dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="72dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/bottomButton"
            android:layout_marginBottom="32dp"
            android:layout_width="28dp"
            android:layout_height="28dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/fabEndButton"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="32dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"/>

    <Constraint
            android:id="@+id/ivOrb"
            android:layout_width="6dp"
            android:layout_height="6dp"
            android:visibility="gone"
            android:alpha="0"
            android:layout_marginTop="8dp"
            motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
            motion:layout_constraintStart_toStartOf="@id/middleButton"
            motion:layout_constraintEnd_toEndOf="@id/middleButton"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/endAtTop">
    <Constraint
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="1"/>

    <Constraint
            android:id="@+id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>

    <Constraint
            android:id="@+id/ivBottomAnchor"
            android:layout_height="1dp"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_width="match_parent"/>

    <Constraint
            android:id="@+id/flBottom"
            motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivLeftAnchor"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flLeft"
            motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
            android:layout_width="300dp"
            android:layout_marginEnd="10dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flRight"
            android:layout_width="0dp"
            motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/fabStartButton"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:alpha="0"
            android:layout_marginBottom="32dp"
            android:layout_marginStart="80dp"
            android:translationY="-100dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/middleButton"
            android:layout_width="52dp"
            android:translationY="-100dp"
            android:alpha="0"
            android:layout_height="52dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="38dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/bottomButton"
            android:translationY="-100dp"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:alpha="0"
            motion:layout_constraintTop_toBottomOf="@id/middleButton"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/fabEndButton"
            android:layout_width="26dp"
            android:translationY="-100dp"
            android:alpha="0"
            android:layout_height="26dp"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="80dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"/>

    <Constraint
            android:id="@+id/ivOrb"
            android:visibility="gone"
            android:translationY="-100dp"
            android:layout_width="6dp"
            android:alpha="0"
            android:layout_height="6dp"
            android:layout_marginTop="8dp"
            motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
            motion:layout_constraintStart_toStartOf="@id/middleButton"
            motion:layout_constraintEnd_toEndOf="@id/middleButton"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/endRighty">
    <Constraint
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0"/>

    <Constraint
            android:id="@+id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>

    <Constraint
            android:id="@+id/ivBottomAnchor"
            android:layout_height="1dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"/>

    <Constraint
            android:id="@+id/flBottom"
            motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivLeftAnchor"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flLeft"
            motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
            android:layout_width="300dp"
            android:layout_marginEnd="10dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivRightAnchor"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flRight"
            motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/fabStartButton"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:layout_marginBottom="32dp"
            android:layout_marginStart="80dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/middleButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="38dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/bottomButton"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:alpha="0"
            motion:layout_constraintTop_toBottomOf="@id/middleButton"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/fabEndButton"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="80dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"/>

    <Constraint
            android:id="@+id/ivOrb"
            android:layout_width="6dp"
            android:layout_height="6dp"
            android:layout_marginTop="8dp"
            motion:layout_constraintTop_toBottomOf="@id/fabEndButton"
            motion:layout_constraintStart_toStartOf="@id/fabEndButton"
            motion:layout_constraintEnd_toEndOf="@id/fabEndButton"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/endLefty">
    <Constraint
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="1"/>

    <Constraint
            android:id="@+id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>

    <Constraint
            android:id="@+id/ivBottomAnchor"
            android:layout_height="1dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"/>

    <Constraint
            android:id="@+id/flBottom"
            motion:layout_constraintTop_toBottomOf="@id/ivBottomAnchor"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivLeftAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flLeft"
            motion:layout_constraintEnd_toStartOf="@id/ivLeftAnchor"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_height="match_parent"
            android:layout_width="1dp"/>

    <Constraint
            android:id="@+id/flRight"
            android:layout_width="0dp"
            motion:layout_constraintStart_toEndOf="@id/ivRightAnchor"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/tbMain"
            android:layout_height="0dp"/>

    <Constraint
            android:id="@+id/fabStartButton"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:alpha="0"
            android:layout_marginBottom="32dp"
            android:layout_marginStart="80dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/middleButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:alpha="0"
            motion:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="38dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/bottomButton"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:alpha="0"
            motion:layout_constraintTop_toBottomOf="@id/middleButton"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"/>

    <Constraint
            android:id="@+id/fabEndButton"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:alpha="0"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="80dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"/>

    <Constraint
            android:id="@+id/ivOrb"
            android:alpha="0"
            android:layout_width="6dp"
            android:layout_height="6dp"
            android:layout_marginTop="8dp"
            motion:layout_constraintTop_toBottomOf="@id/fabStartButton"
            motion:layout_constraintStart_toStartOf="@id/fabStartButton"
            motion:layout_constraintEnd_toEndOf="@id/fabStartButton"/>
</ConstraintSet>

解决方案是等待新版本的 motionLayout。在那个 RC(我认为是 RC3)中有一个关于可见性状态等的错误。从下一个 RC (RC4) 开始,这个问题已经解决,并且按预期工作。