首页 > 解决方案 > MotionLayout - 一个过渡禁用另一个

问题描述

我在一个 MotionScene 中有两个过渡:

<Transition
        android:id="@+id/swipeRightTransition"
        app:constraintSetEnd="@id/swipedRight"
        app:constraintSetStart="@+id/defaultPosition" >

        <OnSwipe
            app:maxAcceleration="60"
            app:dragDirection="dragRight"
            app:onTouchUp="autoComplete"
            app:touchAnchorId="@id/pieChart"
            app:touchAnchorSide="right"/>

    </Transition>

    <Transition
        android:id="@+id/swipeLeftTransition"
        app:constraintSetEnd="@id/swipedLeft"
        app:constraintSetStart="@+id/defaultPosition" >

        <OnSwipe
            app:maxAcceleration="60"
            app:dragDirection="dragLeft"
            app:onTouchUp="autoComplete"
            app:touchAnchorId="@id/pieChart"
            app:touchAnchorSide="left"/>

    </Transition>

如果您使用 getTransition(R.id.swipeRightTransition).setEnable(false) 禁用第一个过渡“swipeRightTransition”,则第二个也将被禁用!并且将“setEnable(true)”设置为第二个无效,无论如何它都会被禁用。如果您将禁用第二个转换“swipeLeftTransition”,则第一个转换工作正常。我完全不明白。

完整场景代码:

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

    <ConstraintSet android:id="@+id/defaultPosition">
        <Constraint android:id="@+id/pieChart"
            android:layout_width="0dp"
            android:layout_height="220dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/leftArrowLayout"
            app:layout_constraintEnd_toStartOf="@id/rightArrowLayout"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_gravity="center"/>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/swipedLeft">
        <Constraint android:id="@+id/pieChart"
            android:layout_width="220dp"
            android:layout_height="220dp"
            app:layout_constraintEnd_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/swipedRight">
        <Constraint android:id="@+id/pieChart"
            android:layout_width="220dp"
            android:layout_height="220dp"
            app:layout_constraintStart_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </ConstraintSet>

    <Transition
        android:id="@+id/swipeRightTransition"
        app:constraintSetEnd="@id/swipedRight"
        app:constraintSetStart="@+id/defaultPosition" >

        <OnSwipe
            app:maxAcceleration="60"
            app:dragDirection="dragRight"
            app:onTouchUp="autoComplete"
            app:touchAnchorId="@id/pieChart"
            app:touchAnchorSide="right"/>

    </Transition>

    <Transition
        android:id="@+id/swipeLeftTransition"
        app:constraintSetEnd="@id/swipedLeft"
        app:constraintSetStart="@+id/defaultPosition" >

        <OnSwipe
            app:maxAcceleration="60"
            app:dragDirection="dragLeft"
            app:onTouchUp="autoComplete"
            app:touchAnchorId="@id/pieChart"
            app:touchAnchorSide="left"/>

    </Transition>
</MotionScene>

请帮忙!

标签: androidandroid-motionlayout

解决方案


这是一个错误/功能。

第一个转换是默认转换。所以一开始你就处于过渡期。哪个被禁用。所以它不能切换到另一个转换,因为你卡在一个被禁用的转换上。简单的解决方案是同时设置另一个过渡。

所以:

ml.setTransition(R.id.swipeLeftTransition);
ml.getTransition(R.id.swipeRightTransition).setEnable(false)

用这个测试:

<?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
    android:id="@+id/sLeft"
    motion:constraintSetStart="@+id/start"
    motion:constraintSetEnd="@+id/left">
    <OnSwipe
        motion:touchAnchorId="@id/view"
        motion:dragDirection="dragLeft" />
</Transition>
<Transition
    android:id="@+id/sRight"
    motion:constraintSetStart="@+id/start"
    motion:constraintSetEnd="@+id/right">
    <OnSwipe
        motion:touchAnchorId="@id/view"
        motion:dragDirection="dragRight" />
</Transition>

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

</ConstraintSet>

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

    <Constraint
        android:id="@+id/view"
        android:layout_width="64dp"
        android:layout_height="64dp"

        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"/>

</ConstraintSet>

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

    <Constraint
        android:id="@+id/view"
        android:layout_width="64dp"
        android:layout_height="64dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintRight_toRightOf="parent"

        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"/>

</ConstraintSet>

如果我打电话

     onAttachedToWindow() {
    super.onAttachedToWindow();
     
    motionLayout.setTransition(R.id.sRight);
    motionLayout.getTransition(R.id.sLeft).setEnable(false);
}

我只能向右滑动

唯一的另一个问题是,如果你处于“左”状态,你就被卡住了


推荐阅读