除了 MotionLayout 过渡之外,还添加了滑动手势

问题描述

我正在使用带有 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...