问题
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
).
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.
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?
回答1:
Thats code slides up your bottom navigation that are inside of a Coordinator Layout and have a HideBottomViewOnScrollBehavior.
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)
回答2:
You can hide bottomNavigation
view from your fragment container activity. Because of you have bottomNavigation
id. and you can access this id from your fragment container activity
and also you can get the current fragment instance from your fragment container activity
. So from your example if you want to hid bottom navigation from particular fragment
onResume()
event you can check that which fragment is currently visible from you Fragment container activity and call hide or show bottom navigation.
Remember for hiding or showing bottom navigation use animation.
来源:https://stackoverflow.com/questions/58412056/programmatically-expand-collapse-bottom-navigation-view-in-coordinatorlayout