android - 在片段上滚动时如何隐藏工具栏
问题描述
我有一个主要活动:
<?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 的工具栏隐藏。我能怎么做?
我在同一个活动中尝试使用回收器,但我不想这样做,我想要不同的片段,因为我有一个底部导航来控制不同的片段。
太感谢了。
解决方案
我刚刚遇到您的问题,但唯一的区别是我在列表视图中滚动。无论如何,我认为该解决方案也可以应用于您的 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>
不要忘记在片段中初始化工具栏
推荐阅读
- arrays - 二分搜索的性能比线性差
- batch-file - 使用 os execute 回显到现有的批处理文件中
- vue.js - 使用 Vue 测试工具全局模拟数据
- python-3.x - 尝试修改熊猫数据框值时出错
- vue.js - 每次在列表中添加新的 I 项时,Vuejs facebook 发送到 messenger 插件都会重新渲染
- android - 如何从 Google API 恢复 OAuth 客户端?
- go - 如何获得相关系数的P值
- python - 如何自动创建 n 个子图(箱线图)?
- python - 过滤具有特定条件的集合列表
- react-native - 在与 Axios 调用的连接不良时正确重试或出错