【问题标题】:Add swipe gestures in addition to MotionLayout transition除了 MotionLayout 过渡添加滑动手势
【发布时间】:2021-04-05 18:52:10
【问题描述】:
我正在使用带有dragUp 转换的 MotionLayout 设置来切换视图上的某些布局。 (基本上你可以向上拖动以查看更多信息。)但是,我也希望能够支持“向左滑动”和“向右滑动”手势来触发应用程序中的其他内容,但我不知道如何收听滑动事件不会破坏我的 MotionLayout 过渡。
如果我在视图上设置 OnTouchListener,我可以看到滑动事件,但这似乎破坏了 MotionLayout 转换,因为我无法再向上拖动以获取额外信息。
我想我可能需要一些方法来在我的 OnTouchListener 向上/向下滑动事件时“通过”它们,但只是调用 super.onFling... 似乎不起作用。否则我希望有一种不同的方式来实现我还没有偶然发现的这种滑动功能......
如有任何建议,我们将不胜感激!
【问题讨论】:
标签:
android
android-layout
android-motionlayout
android-gesture
android-touch-event
【解决方案1】:
你只需要像这样添加更多的约束集和过渡:
<?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。