RecyclerView 阻止在 MotionLayout 中滑动

RecyclerView blocks swipe in MotionLayout

我在回收站视图前面(添加文件夹类对话框)的视图中滑动时遇到问题。当我执行滑动手势时,它只是在它后面的 RecyclerView 中滚动。

然而,当我尝试将 RecyclerView 的可见性设置为 GONE 时,效果很好,所以我怀疑焦点有问题或者 Recycler it self,虽然我在场景改变时清除焦点。

这是video without RecyclerView

这是video with the RecyclerView

显示设置和文件夹对话框的方法:

private void showSettings(){
        if (!settingBox.hasFocus()){
            settingBox.requestFocus();
        }

        foldersGridRecyclerView.clearFocus();


        root.transitionToState(R.id.settingsShowed);
    }

    private void showAddFolderBox(){
        if (!addFolderBox.hasFocus()){
            addFolderBox.requestFocus();
        }

        foldersGridRecyclerView.clearFocus();

        root.transitionToState(R.id.addFolderBoxShowed);
    }

这是我的图表文件:

<?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/addFolderBoxHidden">
        <Constraint
            android:id="@+id/addFolderBox"
            android:layout_width="match_parent"
            android:layout_height="330dp"
            android:layout_marginBottom="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/add_folder_background"
            android:paddingHorizontal="17dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"/>

        <Constraint
            android:id="@+id/settingsBox"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_marginTop="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/settings_bar_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            android:translationZ="6dp"/>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/addFolderBoxShowed">
        <Constraint
            android:id="@+id/addFolderBox"
            android:layout_width="match_parent"
            android:layout_height="330dp"
            android:layout_marginBottom="-330dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/add_folder_background"
            android:paddingHorizontal="17dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"/>

        <Constraint
            android:id="@+id/settingsBox"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_marginTop="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/settings_bar_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            android:translationZ="6dp"/>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/settingsHidden">
        <Constraint
            android:id="@+id/addFolderBox"
            android:layout_width="match_parent"
            android:layout_height="330dp"
            android:layout_marginBottom="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/add_folder_background"
            android:paddingHorizontal="17dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"/>

        <Constraint
            android:id="@+id/settingsBox"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_marginTop="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/settings_bar_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            android:translationZ="6dp"/>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/settingsShowed">
        <Constraint
            android:id="@+id/addFolderBox"
            android:layout_width="match_parent"
            android:layout_height="330dp"
            android:layout_marginBottom="1dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/add_folder_background"
            android:paddingHorizontal="17dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"/>
        <Constraint
            android:id="@+id/settingsBox"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_marginTop="-600dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@drawable/settings_bar_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            android:translationZ="6dp"/>
    </ConstraintSet>

    <Transition
        app:constraintSetEnd="@+id/addFolderBoxHidden"
        app:constraintSetStart="@+id/addFolderBoxShowed" >
        <OnSwipe
            app:touchAnchorId="@id/addFolderBox"
            app:touchRegionId="@id/addFolderBox"
            app:dragDirection="dragUp"
            app:touchAnchorSide="bottom" />

    </Transition>

    <Transition
        app:constraintSetEnd="@+id/settingsHidden"
        app:constraintSetStart="@+id/settingsShowed" >
        <OnSwipe
            app:touchAnchorId="@id/settingsBox"
            app:touchRegionId="@id/settingsBox"
            app:dragDirection="dragDown"
            app:touchAnchorSide="top" />

    </Transition>

    <Transition
        app:constraintSetStart="@+id/addFolderBoxShowed"
        app:constraintSetEnd="@+id/addFolderBoxHidden">

    </Transition>

    <Transition
        app:constraintSetStart="@+id/settingsHidden"
        app:constraintSetEnd="@+id/settingsShowed">

    </Transition>

</MotionScene>

最后 activity 布局:

<?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/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/backgroundColor"
    tools:context=".activities.MainActivity"
    app:layoutDescription="@xml/activity_main_scene">

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/group_grid_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:numColumns="2"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout">

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/addFolderBox"
        android:layout_width="match_parent"
        android:layout_height="330dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginBottom="1dp"
        android:background="@drawable/add_folder_background"
        android:paddingHorizontal="17dp"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"><.../>

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/settingsBox"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_marginTop="1dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="@drawable/settings_bar_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        android:translationZ="6dp"></...>

在 onswipe 中尝试添加:

app:nestedScrollFlags="disableScroll"

这将告诉 MotionLayout 忽略 motionLayout 内的 RecyclerView (nestedScrollView)。

这可能就是您想要的行为。 您可能需要在顶部和底部有一些 UI 达到峰值。否则,您将始终获取 RecyclerView。 其他人在顶部和底部放置了不可见的视图以供抓取。

这只是解决这个问题的错误方法。

制作这样的东西的更好方法是制作这些面板 BottomSheetDialog 并禁用覆盖。