首页 > 解决方案 > 在片段上滚动时如何隐藏工具栏

问题描述

我有一个主要活动:

    <?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <include layout="@layout/toolbar" />

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

        <FrameLayout
            android:id="@+id/frameInicio"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adView"
            android:layout_alignParentStart="true"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:orientation="horizontal" />

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/navigation"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:gravity="center"
            android:visibility="visible"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="example" />


        <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_navigation" />

    </RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在 FrameLayout "frameInicio" 我用这个加载一个片段:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeApuestas"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerApuestas"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </RelativeLayout>

我想要在 RecyclerView 中滚动时,main_activity_layout 的工具栏隐藏。我能怎么做?

我在同一个活动中尝试使用回收器,但我不想这样做,我想要不同的片段,因为我有一个底部导航来控制不同的片段。

太感谢了。

标签: androidhidetoolbar

解决方案


我刚刚遇到您的问题,但唯一的区别是我在列表视图中滚动。无论如何,我认为该解决方案也可以应用于您的 recyclerview。

  1. 首先,这就是您的 fragment.xml 的样子

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
      <androidx.appcompat.widget.Toolbar
          android:id="@+id/tool_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:layout_scrollFlags="enterAlways|scroll|snap">
      </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeApuestas"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerApuestas"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>            </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
  2. 不要忘记在片段中初始化工具栏


推荐阅读