How to show/hide BottomAppBar programmatically?

非 Y 不嫁゛ 提交于 2021-02-07 02:56:02

问题


I try to use an BottomAppBar and I would like to be able to hide or show it programmatically like a setExpanded in an AppBarLayout.

My layout is like

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    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">
          ...
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nested"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment
            android:id="@+id/navHost"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/home_nav" />
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="@drawable/ic_menu"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I tried to use the behavior of the AppBottomBar but it doesn't work.


回答1:


Calling performShow() and performHide() on the bottom app bar will do what you want.




回答2:


You can use slideUp(...) and slideDown(...) methods from its behavior class. For instance:

Kotlin

 val bottomAppBar = ...
 val behavior = bottomAppBar.behavior as HideBottomViewOnScrollBehavior
 behavior.slideDown(bottomAppBar) // use this to hide it
 behavior.slideUp(bottomAppBar) // use this to show it

Java

 BottomAppBar bottomAppBar = ...
 HideBottomViewOnScrollBehavior behavior = (HideBottomViewOnScrollBehavior) bottomAppBar.behavior;
 behavior.slideDown(bottomAppBar) // use this to hide it
 behavior.slideUp(bottomAppBar) // use this to show it



回答3:


Kotlin

class HideBottomNavigationOnScrollBehavior<V : View>(
        context: Context?,
        attrs: AttributeSet?
) : HideBottomViewOnScrollBehavior<V>(context, attrs) {

    public override fun slideDown(child: V) {
        super.slideDown(child)
    }

    public override fun slideUp(child: V) {
        super.slideUp(child)
    }   
}


来源:https://stackoverflow.com/questions/51456312/how-to-show-hide-bottomappbar-programmatically

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