简体   繁体   English

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

[英]How to hide toolbar when scrolling on Fragment

I have a main activity:我有一个主要活动:

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

In FrameLayout "frameInicio" I load a fragment with this:在 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>

I want when scroll in RecyclerView in Fragment, Toolbar of main_activity_layout, hides.我希望在片段中的 RecyclerView 中滚动时,main_activity_layout 的工具栏隐藏。 How can I do?我能怎么做?

I tried with recycler in the same activity, but i don't wanna do this, I want with diferent fragment, because I have a bottom navigation to control diferent fragment.我在同一个活动中尝试使用回收器,但我不想这样做,我想要不同的片段,因为我有一个底部导航来控制不同的片段。

Thank you so much.非常感谢。

I have just faced your problem but the only difference is I'm scrolling within listview.我刚刚遇到了您的问题,但唯一的区别是我在列表视图中滚动。 Anyway I think that solution can be applied on your recyclerview too.无论如何,我认为该解决方案也可以应用于您的回收站视图。

  1. first this is how your fragment.xml should look like首先这是你的 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. don't forget to initialize the toolbar within the fragment不要忘记在片段中初始化工具栏

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

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