使用RecyclerView作为MotionLayout的子级是否有问题?



因此,我尝试创建一个由片段容器和另外两个视图组成的运动场景,以使用滑动手势进行播放。 问题是,每当片段容器包含回收器视图时,如果存在 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 片段中有一个回收器视图并尝试滚动时,我都会受到以下打击

java.lang.NullPointerException: Try to invoke virtual method 'int android.view.View.getId((' 在空对象引用上 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(

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

似乎是约束布局版本 2.0.0-beta7 的问题

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

最新更新