Basically, I have a MotionLayout with a RecyclerView and an ImageView inside. I want the ImageView to act as a header for the RecyclerView and move out of the view when scrolling down the recycler view.
Currently, I have the motion layout making the imageview invisible while the user scrolls down and makes the margin above the recyclerview reduce to 0dp, which is what I want. However, scrolling back up reverses the animation automatically, regardless of position in the recycler view, causing the margin and image to reappear and hiding the contents of the recycler view until the reverse of the animation is completed. I only want the margin/image to reappear above the first item in the recycler view. You can see this in the GIF below that the margin causes the first item to be cut off if it is initially scrolled out of view.
I've been trying to play with the OnSwipe attributes but I've had no luck.
Here is my code:
MotionLayout:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/theme_background"
app:layoutDescription="@xml/fragment_scene"
tools:context=".App.Fragment">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:elevation="2dp"
android:text="BACK TO TOP"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:elevation="1dp"
android:scaleType="centerCrop"
tools:src="@tools:sample/backgrounds/scenic"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Space
android:id="@+id/spacer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="1dp"
android:clipToPadding="false"
android:paddingBottom="?actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/spacer" />
</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:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="1000">
<OnSwipe
motion:dragDirection="dragUp"
motion:dragScale="1"
motion:moveWhenScrollAtTop="false"
motion:onTouchUp="stop"
motion:touchAnchorId="@id/recyclerview"
motion:touchAnchorSide="top"/>
</Transition>
<ConstraintSet android:id="@+id/start">
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/image_view"
android:elevation="1dp"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_width="match_parent"
android:layout_height="300dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintStart_toStartOf="parent"
android:visibility="invisible" />
<Constraint
motion:layout_constraintEnd_toEndOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="0dp"
android:id="@+id/spacer" />
</ConstraintSet>
</MotionScene>
BTW I'm using androidx.constraintlayout:constraintlayout:2.1.0-beta02 for MotionLayout
Edit: Showing how app behaves with motion:moveWhenScrollAtTop="true"