首页 > 解决方案 > 为什么我的工具栏在滚动时没有折叠?

问题描述

我希望我的工具栏在向下滚动此片段中的回收站视图时折叠/隐藏,但是当尝试它在其他帖子中所说的内容时,它仍然没有滚动。我试图添加app:layout_scrollFlags="scroll|enterAlways"到我的工具栏以及app:layout_behavior="@string/appbar_scrolling_view_behavior"我的 viewpager,但似乎都不起作用。这是我的代码:

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"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black">


            <!--- Top Navigation ToolBar  -->

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/menu_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_scrollFlags="scroll|enterAlways"></androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>

            <!--- Bottom Navigation Bar  -->
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_gravity="bottom">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tab_layout"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="center_horizontal"
                    android:background="@color/colorPrimary"
                    app:tabGravity="fill"
                    app:tabIconTint="@android:color/black"
                    app:tabIndicatorColor="@color/colorPrimaryDark"
                    app:tabIndicatorGravity="bottom"
                    app:tabIndicatorHeight="5dp"
                    app:tabMode="fixed"
                    app:tabRippleColor="@color/colorPrimaryDark"
                    app:tabSelectedTextColor="@android:color/black">

                </com.google.android.material.tabs.TabLayout>
            </com.google.android.material.appbar.AppBarLayout>

            <!--- Options for View Pager -->

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/app_bar"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

            </androidx.viewpager.widget.ViewPager>

        </RelativeLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"></com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

fragment_artist_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ArtistHomeFragment"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- Artist Tab Recycler View --> 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/artist_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

标签: androidxmlandroid-toolbar

解决方案


以下代码在我的项目中对我有用...只需尝试调整您的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
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"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:toolbarId="@+id/toolbar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
    />

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/customRecycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/app_bar"
    /> </androidx.coordinatorlayout.widget.CoordinatorLayout>

推荐阅读