android - 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>
请帮忙!
解决方案
这是一个错误/功能。
第一个转换是默认转换。所以一开始你就处于过渡期。哪个被禁用。所以它不能切换到另一个转换,因为你卡在一个被禁用的转换上。简单的解决方案是同时设置另一个过渡。
所以:
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);
}
我只能向右滑动
唯一的另一个问题是,如果你处于“左”状态,你就被卡住了
推荐阅读
- java - 当不要求输入时,键入的文本会显示在控制台中,我该如何阻止它?
- python - Django 模板不会呈现表单错误
- ios - Objective-C:在不可见的自定义 UITableViewCell 上获取属性的值
- docker - keycloak /opt/jboss/tools/docker-entrypoint.sh 出错:第 47 行:KEYCLOAK_FRONTEND_URL
- c++ - 打印给定数字范围内的素数。我无法使代码工作
- java - 我有一个关于对象数组并用 scan.nextLine() 填充的基本 java 问题
- python - 列出 4 个随机数 (0-9) 可以等于 10 的所有方式(使用任何运算符 +、-、*、/)
- django - 关于路径和自定义装饰器的 Python3 Django 问题
- java - 为什么这会引发空点异常?
- php - php 图像压缩 gif 无损失动画