android – MotionLayout with Viewpager's Recyclerview-ThrowExceptions

Exception or error:

I have a layout as such;

<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:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
app:layoutDescription="@xml/motion_layout_details">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navigationIcon="@drawable/ic_back"
    app:title="ToolbarTitle" />

 <androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/image"
    android:layout_width="0dp"
    android:layout_height="244dp"
    android:scaleType="fitXY" />

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tl_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    app:tabIndicator="@null"
    app:tabMode="scrollable"
    app:tabRippleColor="@null" />

<androidx.viewpager.widget.ViewPager
    android:id="@+id/vp_menu"
    android:layout_width="0dp"
    android:layout_height="0dp" />

</androidx.constraintlayout.motion.widget.MotionLayout>

With identical viewpager items that contain a recyclerview;

<androidx.constraintlayout.widget.ConstraintLayout 
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="@color/white"
android:orientation="vertical"
android:paddingTop="@dimen/spacing_16dp">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingBottom="@dimen/spacing_24dp"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

I have a motion scene that collapses the image when scrolled up (the configurations for this are correct). If in my viewpager items I wrap my RecyclerView in a nestedscrollview my transition –

   <Transition
    app:constraintSetEnd="@+id/end"
    app:constraintSetStart="@+id/start"
    app:duration="500"
    app:motionInterpolator="linear">

    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@+id/vp_menu"
        app:touchAnchorSide="top" />

</Transition>

Works just fine, and transitions as I’d expect. But removing that nested scrollview no longer triggers the motion layout at all. I’ve dug around for a good couple of hours and can’t seem to find any explanation. I could put the nestedscrollview back in, but ideally I’d like to leave it out as A) it’s not needed and B) breaks some stickheader stuff I’m using.

UPDATE
Fixed this as per my comment. .isNestedScrollingEnabled = false stops the recyclerview communicating with the motionlayout.

How to solve:

Turns out, from when I had my recyclerview in a nestedscrollview I had rv?.isNestedScrollingEnabled = false which must be stopping some kind of trigger for the motionlayout. Removing this has fixed the issue and works with the above code.

Leave a Reply

Your email address will not be published. Required fields are marked *