错误的锚点触发了MotionLayout OnSwipe过渡

时间:2019-04-17 09:41:34

标签: android android-constraintlayout android-motionlayout

因此,我们试图模仿可窥视的BottomSheetLayout的行为(从屏幕底部窥视的底部视图,向上滚动时会进入全屏视图,类似于:https://www.youtube.com/watch?v=yrZVLL-z6P4)使用MotionLayout版本2.0.0-alpha4

问题在于可窥视视图上的OnSwipe过渡不适用于视图本身,显然适用于其下的整个视图,在本例中为RecyclerView。因此,当我们滚动RecyclerView时,过渡正在运行。

root_view.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/motion_scene">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/margin_48"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_48"
            android:elevation="@dimen/elevation_12"
            android:gravity="center"
            app:layout_constraintBottom_toBottomOf="parent" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="@dimen/elevation_12"
            android:rotation="90"
            app:layout_constraintBottom_toBottomOf="@id/btn"
            app:layout_constraintEnd_toEndOf="@id/btn"
            app:layout_constraintTop_toTopOf="@id/btn" />

        <FrameLayout
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="@dimen/elevation_8"
            app:layout_constraintTop_toBottomOf="parent" />
    </androidx.constraintlayout.motion.widget.MotionLayout>
</FrameLayout>

motion_scene.xml

<Transition
    android:id="@+id/swipe"
    app:constraintSetEnd="@id/end"
    app:constraintSetStart="@id/start"
    app:duration="250">

    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@id/btn" />
</Transition>

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

    <Constraint
        android:id="@id/fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="parent" />

    <Constraint
        android:id="@id/btn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_48"
        app:layout_constraintBottom_toBottomOf="parent" />
</ConstraintSet>

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

    <Constraint
        android:id="@id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn" />

    <Constraint
        android:id="@id/btn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_48"
        app:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>

2 个答案:

答案 0 :(得分:1)

同时使用app:touchAnchorIdapp:touchRegionId对我有帮助。

<OnSwipe
    app:dragDirection="dragUp"
    app:touchAnchorId="@id/btn" 
    app:touchRegionId="@id/btn"/>

答案 1 :(得分:0)

您必须禁用嵌套滚动,才能停止Recyclerview触发动画。请执行以下操作,您会没事的:)

      <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/margin_48"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:nestedScrollingEnabled="false" />