如何在Fragment滚动时隐藏工具栏

3

我有一个主活动:

    <?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>

我希望当在Fragment中滚动RecyclerView时,主活动布局的工具栏会隐藏。我该怎么做?
我尝试过在同一活动中使用recycler,但我不想这样做,我想使用不同的片段,因为我有一个底部导航来控制不同的片段。
非常感谢。

你找到解决方案了吗?我现在也面临着同样的问题。 - Waldmann
2个回答

0

试试这个:

<?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:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardActivity">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"

        />
    <FrameLayout android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_gravity="bottom"
        android:id="@+id/navigation"
        app:labelVisibilityMode="labeled"
        android:background="?android:attr/windowBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/menu_nav"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

针对您的主活动(而非片段)


0

我刚刚遇到了你的问题,但唯一的区别是我在ListView中滚动。无论如何,我认为这个解决方案也可以应用于你的RecyclerView。

首先,这是你的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>

不要忘记在fragment中初始化toolbar。

你还需要让你的 style.xml 文件看起来像这样:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 在这里自定义你的主题。 --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> - Ahmed Gamal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接