【问题标题】:Is there a problem is using a RecyclerView as a child of MotionLayout?将 RecyclerView 用作 MotionLayout 的子项是否存在问题?
【发布时间】:2020-06-15 18:42:59
【问题描述】:

因此,我正在尝试创建一个由片段容器和其他两个视图组成的动态场景,以使用滑动手势进行播放。 问题是,只要片段容器包含回收器视图,并且存在 OnSwipe 转换,应用程序就会崩溃并出现空指针异常。 这是我的代码

这是包含 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>

这是场景文件

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

每当我在 navHost 片段中有一个 RecyclerView 并尝试滚动时,我都会被以下内容击中

java.lang.NullPointerException:尝试调用虚拟方法'int android.view.View.getId()' 在空对象引用上 在 androidx.constraintlayout.motion.widget.MotionLayout.onNestedPreScroll(MotionLayout.java:2200) 在 androidx.core.view.ViewParentCompat.onNestedPreScroll(ViewParentCompat.java:386) 在 androidx.core.view.NestedScrollingChildHelper.dispatchNestedPreScroll(NestedScrollingChildHelper.java:322) 在 androidx.recyclerview.widget.RecyclerView.dispatchNestedPreScroll(RecyclerView.java:11326) 在 androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5061) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) 在 android.view.Choreographer.doCallbacks(Choreographer.java:702) 在 android.view.Choreographer.doFrame(Choreographer.java:635) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6682) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

任何想法该怎么做以及为什么会发生这种情况?

【问题讨论】:

    标签: android android-recyclerview android-motionlayout


    【解决方案1】:

    似乎是 ConstraintLayout 版本 2.0.0-beta7 的问题

    问题跟踪器 https://issuetracker.google.com/issues/158941179

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 2020-11-20
      相关资源
      最近更新 更多