Android 如何使工具栏以片段形式滚动内容?

Android 如何使工具栏以片段形式滚动内容?,android,android-recyclerview,fragment,toolbar,Android,Android Recyclerview,Fragment,Toolbar,我有一个带有DroperLayout的活动,其中包含一个包含RecyclerView的片段。如何让工具栏在RecyclerView中滚动而不必加载RecyclerView中的所有项目 这可能吗 我尝试将CoordinatorLayout和FrameLayout(片段容器)包装到NestedScrollView中,并禁用RecyclerView中的嵌套滚动,这会导致RecyclerView加载所有内容,并且速度要慢得多 activity_main.xml <?xml version="1.0

我有一个带有DroperLayout的活动,其中包含一个包含RecyclerView的片段。如何让工具栏在RecyclerView中滚动而不必加载RecyclerView中的所有项目

这可能吗

我尝试将CoordinatorLayout和FrameLayout(片段容器)包装到NestedScrollView中,并禁用RecyclerView中的嵌套滚动,这会导致RecyclerView加载所有内容,并且速度要慢得多

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        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=".ui.MainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
        >

            <androidx.coordinatorlayout.widget.CoordinatorLayout
                    android:id="@+id/toolbar_container"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
            >
                    <com.google.android.material.appbar.AppBarLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:theme="@style/AppTheme.AppBarOverlay"
                            android:elevation="0dp"
                    >

                        <androidx.appcompat.widget.Toolbar
                                android:id="@+id/toolbar"
                                android:layout_width="match_parent"
                                android:layout_height="?attr/actionBarSize"
                                android:background="?attr/colorPrimary"
                                app:popupTheme="@style/AppTheme.PopupOverlay"
                                app:layout_scrollFlags="scroll"
                        />
                    </com.google.android.material.appbar.AppBarLayout>

            </androidx.coordinatorlayout.widget.CoordinatorLayout>

            <FrameLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintTop_toBottomOf="@id/toolbar_container"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
            >
                <include layout="@layout/content_main"/>
            </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".ui.MainActivity">

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        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"
>

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/alimentaries"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:nestedScrollingEnabled="false"
            tools:listitem="@layout/item_alimentary"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

我认为有两种方法可以实现您想要的行为。您可以使用折叠工具栏布局,另一种方法是使活动全屏显示,并添加要在滚动条上折叠的布局,作为
RecyclerView
的标题

下面是一个在Android中实现折叠工具栏的示例

在第二种方法中,首先需要进行全屏活动,然后必须以具有标题的方式实现
RecyclerView
。您可以向
RecyclerView
添加标题。在您的
AndroidManifest.xml
中添加以下内容以进行全屏活动

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

我认为有两种方法可以实现您想要的行为。您可以使用折叠工具栏布局,另一种方法是使活动全屏显示,并添加要在滚动条上折叠的布局,作为
RecyclerView
的标题

下面是一个在Android中实现折叠工具栏的示例

在第二种方法中,首先需要进行全屏活动,然后必须以具有标题的方式实现
RecyclerView
。您可以向
RecyclerView
添加标题。在您的
AndroidManifest.xml
中添加以下内容以进行全屏活动

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>


我认为此链接将帮助您解决问题<代码>约束布局没有关于工具栏滚动行为的实现。将行为设置到回收器视图中并运行。您还需要使用
折叠工具栏布局
来获得滚动行为。:)我忘了提到它应该像当前的Gmail应用程序一样工作,例如,我认为这个链接会帮助你<代码>约束布局没有关于工具栏滚动行为的实现。将行为设置到回收器视图中并运行。您还需要使用
折叠工具栏布局
来获得滚动行为。:)例如,我忘了提到它应该像当前的Gmail应用程序一样工作。例如,我忘了提到它应该像当前的Gmail应用程序一样工作。例如,我忘了提到它应该像当前的Gmail应用程序一样工作