MotionLayout - OnSwipe not working with ViewPager2/RecyclerView

Viewed 1510

The onSwipe gesture does not work with ViewPager2/RecyclerView, however if I replace the ViewPager2/RecyclerView with ImageView, it starts working perfectly.

activity_main.xml

<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"
    app:layoutDescription="@xml/motion_scene">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/glTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/glBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="1.7" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/red"
        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>

motion_scene.xml

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@+id/start"
        motion:duration="300"
        motion:motionInterpolator="easeOut">

        <OnSwipe
            motion:dragDirection="dragDown"
            motion:touchAnchorId="@+id/viewpager"
            motion:touchAnchorSide="bottom" />

    </Transition>

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


    </ConstraintSet>


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

        <Constraint
            android:id="@id/ivPoster"
            motion:layout_constraintBottom_toBottomOf="@+id/glBottom"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@+id/glTop" />

    </ConstraintSet>

</MotionScene>
2 Answers

include you ViewPager into NestedScrollView and make the OnSwipe event on the NestedScrollView rather than ViewPager

Facing same issue today, by searching I've found that it has been disabled in RecyclerView Version 1.1.0-beta04

dx and dy arguments dispatched to nested pre-scrolls are zeroed when RecyclerView can’t scroll in that direction (aosp/1105373)

Related