4

我在我的运动布局中使用了一个回收器视图,它带有一个 ImageView 的句柄。但是当你第一次想要向上滑动它时,它会摇晃并上下移动并且不会出现。

我尝试使用这些属性,但没有任何效果。

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity"
    android:id="@+id/root">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motionLayout"
        app:layoutDescription="@xml/scene3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:showPaths="true">
        <ImageView
            android:id="@+id/image"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@id/testId" />

        <RelativeLayout
            android:id="@+id/testId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintVertical_chainStyle="spread">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_media_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"/>

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/media_send_fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="16dp"
                android:onClick="sendTheMessages"
                app:fabSize="normal" />
        </RelativeLayout>
    </androidx.constraintlayout.motion.widget.MotionLayout>
</RelativeLayout>

场景3.xml:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">
    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@+id/start"
        motion:duration="300"
        motion:interpolator="linear">
        <OnSwipe
            motion:touchAnchorId="@+id/image"
            motion:touchAnchorSide="bottom"
            motion:dragDirection="dragUp"
            motion:dragScale="5"
            motion:maxVelocity="5000"
            motion:moveWhenScrollAtTop="true" />
    </Transition>
    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/testId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="parent"
            motion:layout_constraintVertical_chainStyle="spread">
        </Constraint>
    </ConstraintSet>
    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/testId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintVertical_chainStyle="spread">
        </Constraint>
    </ConstraintSet>
</MotionScene>

使用以下文件,您可以重新生成问题。感谢阅读。任何帮助将不胜感激。

4

2 回答 2

0

最后在 androidx.constraintlayout:constraintlayout 2.0.0-beta4 修复了这个错误

于 2019-12-23T05:35:29.903 回答
0

我在使用 MotionLayout 折叠标题时遇到了类似的问题。滑动性能很糟糕。所以我读了@8KCreativeCommons 评论说这可能是由recyclerview 大小的变化引起的。

设置一个固定的 recyclerview 高度而不是 0dp(匹配约束)提高了滑动性能:

private void setRecyclerViewHeight() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    mRecyclerView.getLayoutParams().height = height;
}

在您的情况下,您必须为您的相对布局 @+id/testId 设置一个固定大小。

还需要删除 layout_constraintBottom_toBottomOf="parent"

于 2022-03-04T15:39:10.827 回答