首页 > 解决方案 > 带有底部工作表的 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:layout_width="match_parent"
 android:layout_height="match_parent"
 app:layoutDescription="@xml/activity_main_scene"
 tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageViewAvatar"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:src="@drawable/ic_baseline_add_reaction_24"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:layout_editor_absoluteX="0dp">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottomSheet"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bottom_view_bg"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="90dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <View
            android:layout_width="80dp"
            android:layout_height="2dp"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:background="@color/white" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvHorzinatal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@layout/item_rv" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            android:text="Swipe Up"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/item_rv" />

    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</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"
xmlns:motion="http://schemas.android.com/apk/res-auto">

<Transition
    motion:constraintSetEnd="@+id/end"
    motion:constraintSetStart="@id/start"
    motion:duration="1000">

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

<ConstraintSet android:id="@+id/start"></ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@id/imageViewAvatar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </Constraint>
</ConstraintSet>

但是每当我将 Onswipe 放在坐标布局或其中的约束上时,它都不起作用我该如何解决这个问题?

标签: androidbottom-sheetandroid-motionlayoutandroid-bottomsheetdialog

解决方案


CoordinatorLayout 是一个用于动画视图运动的系统,与 NestedScrollview(RecyclerView) MotionLayout 可以用于相同的事情。

你有两个选择:

  • 只使用一个 - MotionLayout 更灵活一点,CoordnatorLayout 提供标准交互。
  • 将 MotionLayout 设为子级以仅对顶部面板进行动画处理。CoordnatorLayout 做整体动画。

这里有一些例子


推荐阅读