Programmatically expand/collapse Bottom Navigation View in CoordinatorLayout

拜拜、爱过 提交于 2020-05-31 06:03:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!