繁体   English   中英

如何使“工具栏”与Fragment中的内容一起滚动?

[英]How to make Toolbar scroll with content in Fragment?

我有一个DrawerLayout活动,其中包含一个包含RecyclerView的片段。 如何让ToolBar在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"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM