Search code examples
androidandroid-recyclerviewandroid-motionlayout

Is there a problem is using a RecyclerView as a child of MotionLayout?


So, am trying to create a motionscene that's composed of a fragment container and two other views to play with using swipe gestures. The problem is whenever the fragment container contains a recycler view, with the presence of the OnSwipe transition, the app crashes with a null pointer exception. here's my code

this is the main layout that contains the 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:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_single_scene03"
    tools:context="com.mondiamedia.musicshop.activities.SingleActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/navHost"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true" />


    <include layout="@layout/layout_mini_player" />

    <include layout="@layout/layout_full_player" />
</androidx.constraintlayout.motion.widget.MotionLayout>

and this is the scene file

<?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">

    <ConstraintSet android:id="@+id/startSet">
        <Constraint
            android:id="@+id/miniPlayerContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintWidth_percent="0.9">
            <PropertySet android:alpha="1" />
        </Constraint>
        <Constraint
            android:id="@+id/fullPlayerContainer"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="parent">
            <PropertySet android:alpha="0" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/endSet">
        <Constraint
            android:id="@+id/miniPlayerContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintWidth_percent="0.9">
            <PropertySet android:alpha="0" />
        </Constraint>
        <Constraint
            android:id="@+id/fullPlayerContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <PropertySet android:alpha="1" />
        </Constraint>
    </ConstraintSet>

    <Transition
        app:constraintSetEnd="@id/endSet"
        app:constraintSetStart="@id/startSet"
        app:duration="1000">
        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/miniPlayerContainer"
            app:touchAnchorSide="top"
            app:touchRegionId="@id/miniPlayerContainer" />
    </Transition>

    <Transition
        app:constraintSetEnd="@id/startSet"
        app:constraintSetStart="@id/endSet"
        app:duration="1000">
        <OnSwipe
            app:dragDirection="dragDown"
            app:touchAnchorId="@id/fullPlayerContainer"
            app:touchAnchorSide="top"
            app:touchRegionId="@id/fullPlayerContainer" />
    </Transition>

</MotionScene>

whenever i have a RecyclerView in the navHost fragment, and try to scroll, I got hit by the following

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference at androidx.constraintlayout.motion.widget.MotionLayout.onNestedPreScroll(MotionLayout.java:2200) at androidx.core.view.ViewParentCompat.onNestedPreScroll(ViewParentCompat.java:386) at androidx.core.view.NestedScrollingChildHelper.dispatchNestedPreScroll(NestedScrollingChildHelper.java:322) at androidx.recyclerview.widget.RecyclerView.dispatchNestedPreScroll(RecyclerView.java:11326) at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5061) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) at android.view.Choreographer.doCallbacks(Choreographer.java:702) at android.view.Choreographer.doFrame(Choreographer.java:635) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Any Ideas what to do and why this is happening ?


Solution

  • Seems like an issue with ConstraintLayout release 2.0.0-beta7

    Issue Tracker https://issuetracker.google.com/issues/158941179