簡體   English   中英

在 Fragment 上滾動時如何隱藏工具欄

[英]How to hide toolbar when scrolling on Fragment

我有一個主要活動:

    <?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 的工具欄隱藏。 我能怎么做?

我在同一個活動中嘗試使用回收器,但我不想這樣做,我想要不同的片段,因為我有一個底部導航來控制不同的片段。

非常感謝。

我剛剛遇到了您的問題,但唯一的區別是我在列表視圖中滾動。 無論如何,我認為該解決方案也可以應用於您的回收站視圖。

  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. 不要忘記在片段中初始化工具欄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM