问题描述
我正在使用带有 dragUp
过渡的 MotionLayout 设置来切换视图上的某些布局。 (基本上你可以向上拖动以查看更多信息。)但是,我也希望能够支持“向左滑动”和“向右滑动”手势来触发应用程序中的其他内容,但我不知道如何收听滑动事件而不破坏我的 MotionLayout 过渡。
如果我在视图上设置 OnTouchListener,我可以看到滑动事件,但这似乎破坏了 MotionLayout 过渡,因为我无法再向上拖动以获取额外信息。
我想我可能需要某种方式在 OnTouchListener 上/下时“传递”滑动事件,但仅调用 super.onFling...
似乎不起作用。否则,我希望有一种不同的方式来实现我尚未偶然发现的这种滑动功能......
这里的任何建议将不胜感激!
解决方法
你只需要像这样在它们之间添加更多的 ConstraintSets 和 Transitions:
<?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">
<Transition
app:constraintSetStart="@id/start"
app:constraintSetEnd="@id/middle">
<OnSwipe
app:touchAnchorId="@id/square"
app:dragDirection="dragUp"
app:touchAnchorSide="top"/>
</Transition>
<Transition
app:constraintSetStart="@id/middle"
app:constraintSetEnd="@id/left">
<OnSwipe
app:touchAnchorId="@id/square"
app:dragDirection="dragLeft"
app:touchAnchorSide="left"/>
</Transition>
<Transition
app:constraintSetStart="@id/middle"
app:constraintSetEnd="@id/right">
<OnSwipe
app:touchAnchorId="@id/square"
app:dragDirection="dragRight"
app:touchAnchorSide="left"/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@id/square">
<Layout
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/middle">
<Constraint android:id="@id/square" >
<Layout
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/left">
<Constraint android:id="@id/square" >
<Layout
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/right">
<Constraint android:id="@id/square" >
<Layout
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</Constraint>
</ConstraintSet>
</MotionScene>
如果您想看到一个有趣的工作示例 here's a sliding tile puzzle I did。