androidx / constraintlayout

ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way
Apache License 2.0
1.06k stars 177 forks source link

MotionLayout OnSwipe only animated after first ACTION_UP #808

Closed wiryadev closed 1 year ago

wiryadev commented 1 year ago

Im creating horizontal recyclerview inside motionlayout. I have intercept the OnTouch but the transition only working after first ACTION_UP. So first time the screen is opened, animation not working. After i touch it and lift my fingers up which triggers ACTION_UP, then it is working now.

So the flow is like this: opened the screen -> swipe -> ACTION_MOVE happened, but animation not working -> lift fingers up -> ACTION_UP happened -> swipe again, now animation working.

GIF can be seen here.

The project for reproduce is in here.

Why is it happening and how to solve it?

My onTouchListener

binding.rvPoster.setOnTouchListener { v, event ->
    binding.motionLayoutHorizontal.onTouchEvent(event)
    return@setOnTouchListener false
}

My motionscene:

<?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:constraintSetEnd="@+id/end"
        app:constraintSetStart="@id/start"
        app:duration="1000">
        <KeyFrameSet></KeyFrameSet>
        <OnSwipe
            app:dragDirection="dragEnd"
            app:touchRegionId="@id/rvPoster"
            app:onTouchUp="stop"/>
    </Transition>

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

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/ivLogo"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btnSeeAll" />
        <Constraint
            android:id="@+id/rvPoster"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btnSeeAll" />
        <Constraint
            android:id="@+id/verticalGuideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintGuide_begin="20dp" />
    </ConstraintSet>
</MotionScene>
jafu888 commented 1 year ago

Change onSwipe to:

      <OnSwipe
            app:dragDirection="dragStart"
            app:touchAnchorId="@id/rvPoster"
            app:touchAnchorSide="start"
            app:onTouchUp="stop"/>

comment out:

//            setOnTouchListener { v, event ->
//                Log.d("OnTouchListener", "event: $event")
//                binding.motionLayoutHorizontal.onTouchEvent(event)
////                v.performClick()
//                return@setOnTouchListener false
//            }
wiryadev commented 1 year ago

Thank you for the help, it works smoothly