首页 > 解决方案 > OnSwipe dragUp 不适用于 MotionLayout 中的子视图

问题描述

我想使用 MotionLayout 在我的活动中实现底部工作表。

运动场景:

<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="2000">

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/bottomSheet"
            motion:touchRegionId="@id/bottomSheet" />

    </Transition>

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

        <Constraint
            android:id="@+id/bottomSheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            motion:layout_constraintBottom_toBottomOf="parent"/>

    </ConstraintSet>

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

        <Constraint
            android:id="@+id/bottomSheet"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"/>
    
    </ConstraintSet>

</MotionScene>

现在,如果我像下面的代码那样实现我的 MotionLayout,我不能在按钮区域拖动:

<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/ml"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/bottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/black">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_dark"
            android:padding="40dp">

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="A button" />

        </FrameLayout>

    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.motion.widget.MotionLayout>

我可以MaterialCardViewNestedScrollViewthen 包装,因为它NestedScrollView会正确处理onInterceptTouchEvent,它会起作用:

<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/ml"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/bottomSheet"
        android:background="@android:color/holo_blue_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@android:color/black">

            <!-- other views -->

        </com.google.android.material.card.MaterialCardView>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.motion.widget.MotionLayout>

如果我像这样实现 MotionLayout,我将面临另一个问题。当MaterialCardView用户向上拖动视图时,高度不会改变,即使我将高度设置为match_parent

第二个动作布局

如何以红色区域覆盖整个蓝色区域的方式更改代码?

标签: androidandroid-animationbottom-sheetandroid-motionlayoutandroid-motionscene

解决方案


最后,我找到了解决方案。我只需要添加

android:fillViewport="true"

到 NestedScrollView 来解决这个问题。发生这种情况是因为 NestedScrollView 在最终状态下的高度大于其子项的高度,因此我需要设置fillViewporttrue解决该问题。


推荐阅读