首页 > 解决方案 > MotionLayout - 滑动时的单个连续过渡?

问题描述

我有四种不同的布局,可以通过向左滑动一个接一个地滑动。

当一个过渡完成时,我希望能够在进一步向左滑动时继续进行另一个过渡。向下滑动时,所有布局都有自己的过渡。我目前有很多转换,transitionToState()每次按下按钮时都在设置。

到目前为止,有没有一种方法可以通过滑动动作在 MotionLayout 中实现这一点?A way to chain transitions into one single transition by swiping left each time.

标签: javaandroidkotlinandroid-constraintlayoutandroid-motionlayout

解决方案


如果它不与nestedScrollView(如回收器视图)相关联,简短的回答是肯定的。

通常,如果您有 3 个状态 A、B 和 C 以及两个转换 A->B 和 B->C,并且 OnSwipe 的方向相同。他们自然会在状态 A 下“链接” IE Width touch down,它会将 A 拖到 B,然后将 B 拖到 C。在 B 之前触摸它会在 B 处停止。
没有简单的方法可以防止 B 成为有效停止。

此视频中有关 onSwipe 的更多详细信息

例如这个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/motionLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#FF003b60"
  app:layoutDescription="@xml/scene"
  app:motionDebug="SHOW_PATH">
<View
    android:id="@+id/view"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#5F3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.motion.widget.MotionLayout>

使用 MotionScene 文件:

<?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
    motion:constraintSetStart="@id/a"
    motion:constraintSetEnd="@+id/b">
    <OnSwipe motion:dragDirection="dragRight" />
  </Transition>

  <Transition
    motion:constraintSetStart="@id/b"
    motion:constraintSetEnd="@+id/c">
    <OnSwipe motion:dragDirection="dragRight" />
  </Transition>

  <ConstraintSet android:id="@+id/a">
    <ConstraintOverride android:id="@+id/view"
        motion:layout_constraintHorizontal_bias="0" />
  </ConstraintSet>

  <ConstraintSet android:id="@+id/b"/>

  <ConstraintSet android:id="@+id/c">
    <ConstraintOverride android:id="@id/view"
        motion:layout_constraintHorizontal_bias="1" />
  </ConstraintSet>
  </MotionScene>

这个运动场景有 3 个约束集 a、b 和 c 它还有两个转换 a->b 和 b->c 在滑动时都有


推荐阅读