在滚动时显示和隐藏 BottomAppBar

WWJ*_*WJD 2 android android-layout material-components

我正在尝试实现BottomAppBar我将在何处处理WebView.

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_behavior="com.google.android.material.bottomappbar.BottomAppBar$Behavior"
        app:hideOnScroll="true"
        app:fabAttached="true"
        app:fabAlignmentMode="end"
        app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_share"
    android:tint="#fff"
    app:layout_anchor="@id/bottom_bar"
    app:backgroundTint="@color/colorPrimary"/>
Run Code Online (Sandbox Code Playgroud)

一切正常,但我希望能够显示和隐藏BottomAppBar用户分别向下和向上滚动的时间,就像这里的行为一样。

我在任何地方都找不到关于它的教程,所以有人在上面实施了解决方案吗?

Cam*_*ham 7

尝试设置app:hideOnScroll而不设置app:layout_behaviorapp:layout_scrollFlags。并删除AppBarLayout父视图,因此BottomAppBar它只是CoordinatorLayout.

  • 您需要有一个 `CoordinatorLayout`,将 `RecyclerView`(或实现 `NestedScrollingChild` 的东西)和 `BottomAppBar` 作为直接子元素。然后你只需在 `BottomAppBar` 上设置 `app:hideOnScroll` 来启用该功能。那有意义吗? (2认同)