如何让 NestedScrollView 和 RecyclerView 一起滚动?

How do I make NestedScrollView and RecyclerView scroll together?

所以我在 NestedScrollView 中有一个 RecyclerView,因为我想让我的整个片段都可以滚动。

布局如下:

<FrameLayout 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=".fragments.Recipes">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nested_scroll_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/edit_text_view"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_marginStart="32dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_view_1"
                    android:fontFamily="@font/roboto"
                    android:gravity="center_vertical"
                    android:hint="@string/text1"
                    android:textColorHint="#757575"
                    android:maxLines="1"
                    android:paddingStart="10dp"
                    android:textColor="@android:color/black"
                    app:layout_constraintBottom_toBottomOf="@+id/button_1"
                    app:layout_constraintEnd_toStartOf="@+id/button_1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:ignore="RtlSymmetry" />

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/button_1"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="32dp"
                    android:padding="10dp"
                    android:contentDescription="@string/submit"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/text_view_2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginStart="32dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="32dp"
                    android:drawablePadding="16dp"
                    android:fontFamily="@font/roboto"
                    android:gravity="center_vertical"
                    android:textColorHint="#757575"
                    android:padding="12dp"
                    android:textColor="@android:color/black"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/edit_text_view" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="24dp"
                    android:clipToPadding="false"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/text_view_2"
                    tools:listitem="@layout/example_item" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

如您所见,我的 NestedScrollView 里面还有一个 EditTextButtonTextView。这些将默认显示,但 RecyclerView 仅在满足特定行为后才会填充。当 RecyclerView 被填充时,它显示一个可滚动的列表。我希望屏幕随着 EditTextButtonTextView 滚动。这样,这三个组件就不会固定在屏幕上,当用户向下滚动时,这三个组件将向上移动并消失,直到用户向上滚动。

我该怎么做?

据我所知,您想要一个包含 RecyclerViewEditTextTextView 等随站点滚动的包。但是 RecyclerView 本身仍然可以滚动。我通过更改一些行并在 RecyclerView 下面的末尾放置一个很长的虚拟文本来实现这一点。 TextView 在下端突出。行为是您可以滚动整个页面,如果您在 RecyclerView 内滚动,则只有 RecyclerView 滚动。这就是我得到的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/edit_text_view"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="24dp"
            android:gravity="center_vertical"
            android:hint="text1"
            android:textColorHint="#757575"
            android:maxLines="1"
            android:paddingStart="10dp"
            android:textColor="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/button_1"
            app:layout_constraintEnd_toStartOf="@+id/button_1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="RtlSymmetry" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button_1"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="32dp"
            android:padding="10dp"
            android:contentDescription="submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_view_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="32dp"
            android:drawablePadding="16dp"
            android:gravity="center_vertical"
            android:textColorHint="#757575"
            android:padding="12dp"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/edit_text_view" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvAnimals"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:layout_constraintTop_toBottomOf="@id/text_view_2"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:id="@+id/textview_long"
            tools:ignore="MissingConstraints"
            android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,"
            android:textSize="22sp"
            android:padding="20dp"
            app:layout_constraintTop_toBottomOf="@id/rvAnimals"/>
        
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

在图片中我做了 3 个黄点,证明一切都是可滚动的:


我认为您遇到的主要问题是以错误的方式定义了一些属性,显然您在屏幕末尾没有扩展内容。所以对于 NestedScrollView 没有什么可做的。

因此:您的屏幕足够大,可以容纳内容,滚动变得多余。

问题是我 RecyclerViewNestedScrollView 都有 app:layout_constraintBottom_toBottomOf="parent"。一旦我删除它并将 android:nestedScrollingEnabled="false" 添加到 RecyclerView,它就起作用了。