android - 启用自动隐藏/显示后,BottomNavigationView 不显示
问题描述
我有Activity
一个BottomNavigationView
内部布局的单曲:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:menu="@menu/menu_home_bottom_navigation"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我的bottom_avigation
变化nav_host
FragmentContainerView
与碎片。所有这些片段都有NestedScrollView
或RecyclerView
因为app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
,我bottom_navigation
在 scrollDown/scrollUp 上自动隐藏/显示。
我看到了这个问题:Hide/Show bottomNavigationView on Scroll 。我目前正在使用Abhishek Singh给出的答案,但问题不在于这个。
这是我的问题:想象一下FragA
,FragB
两者都有RecyclerViews
但FragA
项目较少,导致所有项目都适合屏幕且不可滚动。现在,当我从 to 切换FragA
到FragB
然后 scrollDown 时,会bottom_navigation
隐藏动画,如果我按下后退按钮,我就再也看不到bottom_navigation
了,因为FragA
不可滚动,我无法通过滚动使其可见。
我也尝试过bottom_navigation.visibility = View.Visible
,FragA
onResume
但仍然无法正常工作。我认为它以某种方式转化bottom_navigation
为底部,因此此代码无济于事。
那么我该如何解决这个问题呢?
解决方案
我找到了答案。我没有更改 的visibility
属性bottom_navigation
,而是编写了两个扩展函数BottomNavigationView
来隐藏/显示它:
private fun BottomNavigationView.showUp() {
animate().setDuration(200L).translationY(0f).withStartAction { visibility = View.VISIBLE }.start()
}
private fun BottomNavigationView.hideDown() {
animate().setDuration(200L).translationY(height.toFloat()).withEndAction { visibility = View.GONE }.start()
}
现在我有这个onResume
:FragA
override onResume() {
super.onResume()
bottom_navigation.showUp()
}
推荐阅读
- simulink - 在 FMIKit-Simulink 中创建所有当前变量及其各自值的列表
- ms-word - 如何使用openxml合并具有不同标题的word文档?
- python - 如何使用 h5py 模块访问存储在 .mat 文件中的单元格变量?
- javascript - API 数据不能在 innerHTML 中放置数组
- single-page-application - 方便的微前端框架?
- python - 带有 method=None (默认值)的 Pandas fillna 会引发错误
- javascript - 似乎元素就像一个伪窗口元素或视图容器元素?
- flutter - Flutter image_picker 0.4.12+1 未从相机返回
- react-native - React Native - 是否有可靠的地理围栏包可用?
- git - git log:列出两个给定提交“之间”的提交