当滚动隐藏时,如何隐藏锚定到BottomAppBar的FloatingActionButton

时间:2021-03-19 22:51:17

标签: android floating-action-button

FloatingActionButton 由于 BottomAppBar 滚动而隐藏时,我试图隐藏 NestedScrollView。设置 BottomAppBar 后,app:hideOnScroll="true" 隐藏成功,但 FloatingActionButton 保持可见。我的布局如下所示:

<androidx.coordinatorlayout.widget.CoordinatorLayout> 

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

   ...

</androidx.core.widget.NestedScrollView>

<com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="88dp"
        android:layout_gravity="bottom"
        android:layout_marginHorizontal="16dp"
        android:layout_marginVertical="8dp"
        app:backgroundTint="@color/colorWhite"
        android:requiresFadingEdge="vertical"
        app:fabCradleRoundedCornerRadius="16dp"
        app:fabCradleMargin="16dp"
        app:fabCradleVerticalOffset="16dp"
        app:hideOnScroll="true"
        >
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            app:menu="@menu/bottom_menu"
            app:elevation="0dp"
            app:labelVisibilityMode="labeled"
            android:background="@android:color/transparent"
            app:itemTextColor="@color/colorPrimaryApp"
            app:itemIconTint="@color/colorPrimaryApp"
            app:itemBackground="@drawable/bvn_transparent_light_blue_color_selector"
            android:layout_marginEnd="16dp"
            />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bnv_add"
        android:tint="@color/sky"
        android:backgroundTint="@color/sky"
        app:layout_anchor="@+id/bottomAppBar"
        tools:ignore="ContentDescription"
        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

有没有什么绝妙的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

我使用一些技巧设法隐藏了 FloatingActionButton,如下所示:

    val point = Point()
    val startSize = fab.customSize
    nested_view.viewTreeObserver.addOnScrollChangedListener {
        windowManager.defaultDisplay.getSize(point)
        when {
            bottomAppBar.y + bottomAppBar.measuredHeight > point.y ->
                { fab.customSize = 1; fab.alpha = 0.0f}
            else ->
                { fab.customSize = startSize; fab.alpha = 1f}
        }
    }

但我不喜欢它 :) 而且它的行为有点奇怪:

click here to see video