以编程方式在CoordinatorLayout中展开/折叠底部导航视图 [英] Programmatically expand/collapse Bottom Navigation View in CoordinatorLayout
问题描述
我有一个CoordinatorLayout
,其中包含一个BottomNavigationView
和一个AppBarLayout
,其中包含一个ToolBar
. (BottomNavigationView
不在AppBarLayout
内,因为这样做会破坏BottomNavigationView
的位置.)
I have a CoordinatorLayout
that contains a BottomNavigationView
and an AppBarLayout
with a ToolBar
inside of it. (The BottomNavigationView
is not inside the AppBarLayout
, as doing it breaks the position of the BottomNavigationView
).
当某些事件发生时(例如在片段的onResume
上),我需要以编程方式显示/隐藏AppBarLayout
和BottomNavigationView
,到目前为止,我已经管理了设置appBarLayout.setExpanded(true, true)
以便显示/隐藏AppBar,但是我不知道如何为BottomNavigationView
做同样的事情,因为它没有任何显示/隐藏自身的方法.
I need to show/hide the AppBarLayout
and the BottomNavigationView
programmatically, when certain events happen (e.g. on a fragment's onResume
), and so far I've managed setting appBarLayout.setExpanded(true, true)
in order to show/hide the AppBar, but I can't figure out how to do the same for the BottomNavigationView
, as it does not have any method to show/hide itself.
我的BottomNavigationView
的行为是app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
,在布局xml中设置.如何在我的代码中引用此行为以管理其扩展/折叠状态?
The behaviour of my BottomNavigationView
is app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
, set in the layout xml. How can I get a reference to this behaviour in my code in order to manage its expanded/collapsed state?
推荐答案
那是代码将您的底部导航向上滑动到协调器布局内,并具有HideBottomViewOnScrollBehavior.
Thats code slides up your bottom navigation that are inside of a Coordinator Layout and have a HideBottomViewOnScrollBehavior.
如果您使用的是findViewById,只需将其替换为每个binding.
If you are using findViewById just replace that for every binding.
val layoutParams = binding.bottomNavigation.layoutParams as CoordinatorLayout.LayoutParams
val bottomViewNavigationBehavior = layoutParams.behavior as HideBottomViewOnScrollBehavior
bottomViewNavigationBehavior.slideUp(binding.bottomNavigation)
这篇关于以编程方式在CoordinatorLayout中展开/折叠底部导航视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!