3

I tried to set an onSwipe handler on a NestedScrollView to drive the animation in a MotionLayout. However the first time the animation doesn't work. But if I start the animation d, then the onSwipe handler begins to work as expected. Here is the relevant code for the NestedScrollView:

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nsv_desc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="160dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/slider_view"
            android:clipToPadding="true"
            android:background="?colorSurface">

...

        <Constraint android:id="@+id/nsv_desc">
        <Layout
                motion:layout_constraintEnd_toEndOf="parent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                motion:layout_constraintTop_toBottomOf="@id/slider_view"
                motion:layout_constraintStart_toStartOf="parent"
                android:layout_marginTop="@dimen/margin_all_large"
                motion:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="@dimen/margin_all_normal" />
        <CustomAttribute
                motion:attributeName="elevation"
                motion:customDimension="0dp" />
    </Constraint>

...

        <OnSwipe
        motion:touchAnchorId="@id/nsv_desc"
        motion:dragDirection="dragUp"
        motion:touchAnchorSide="top"/>

...

            <KeyAttribute
                motion:motionTarget="@id/nsv_desc"
                motion:framePosition="25"
                android:elevation="4dp"
                android:translationY="40dp" />

EDIT: SOLUTION I fixed the bug. It turned out that it was very specific to my app. For anyone interested: suppose you have multiple ConstraintSets and transitions between them, like A -> B -> C -> D ecc. If you transition (with the onSwipe handler or programmatically) from A to B and reach the state B, then you have to call motionLayout.setTransition to specify that your motion layout must now apply the transition from B to C when you swipe on the widgets. If you call the transition from B to C programmatically it automatically applies that transition (that's the reason of the behavior described in the question).

iClaude
  • 707
  • 8
  • 14

0 Answers0