MotionLayout - 一个转换禁用另一个
MotionLayout - one transition disabling another
我在一个 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>
请帮忙!
这是一个bug/feature。
第一个过渡是默认过渡。所以一开始你就处于那个过渡期。
哪个被禁用。所以它不能切换到另一个转换,因为你被困在一个被禁用的转换上。
简单的解决方法是同时设置另一个transition。
所以:
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);
}
我只能向右滑动
唯一的另一个问题是,如果你处于“左”状态,你就会被卡住
我在一个 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>
请帮忙!
这是一个bug/feature。
第一个过渡是默认过渡。所以一开始你就处于那个过渡期。 哪个被禁用。所以它不能切换到另一个转换,因为你被困在一个被禁用的转换上。 简单的解决方法是同时设置另一个transition。
所以:
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);
}
我只能向右滑动
唯一的另一个问题是,如果你处于“左”状态,你就会被卡住