Motion layout with RecyclerView, scroll at the end of recyclerview

751 Views Asked by At

Any ideas how to implement scrollToPosition with MotionLayout?

Currently I have a RecyclerView and some header that hides on scroll and shows if you scroll to top. This is the scene:

<Transition
    motion:constraintSetEnd="@+id/end"
    motion:constraintSetStart="@id/start"
    motion:duration="1000">
    <OnSwipe
        motion:touchAnchorId="@+id/header"
        motion:dragDirection="dragUp"
        motion:onTouchUp="stop" />
</Transition>

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

    <Constraint
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="visible"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/toolbar" />

    <Constraint
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/header" />

</ConstraintSet>

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

    <Constraint
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        motion:layout_constraintBottom_toTopOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent" />

    <Constraint
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@+id/toolbar" />
</ConstraintSet>

And when an Item is inserted into the RecyclerView, I want it to scroll to the bottom so I wrote this:

private fun scrollToBottom() {
    val position = routineSetsAdapter.itemCount - 1
    binding.motionLayoutContainer.transitionToEnd()
    binding.recyclerView.scrollToPosition(position)
}

And it works great for a few items but then after adding 10 items and if you are on top and you add an item, the RecyclerView scrolls, but it is not in sync with MotionLayout. Is there any way to scroll with motion layout the recyclerView programatically. If you scroll on your own it works like it should.

1

There are 1 best solutions below

1
On

Try recyclerView.smoothScrollToPosition(position);