【问题标题】:How to hide FloatingActionButton which is anchored to BottomAppBar when the is hidden on scroll滚动隐藏时如何隐藏锚定到BottomAppBar的FloatingActionButton
【发布时间】:2021-06-17 08:32:27
【问题描述】:

由于NestedScrollView 滚动,当BottomAppBar 被隐藏时,我试图隐藏FloatingActionButton。设置app:hideOnScroll="true"BottomAppBar 隐藏成功,但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>

有没有什么绝招?

【问题讨论】:

    标签: android floating-action-button


    【解决方案1】:

    我设法使用一些技巧隐藏了 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 2016-01-17
      • 2019-07-05
      • 2018-01-19
      • 2015-09-25
      • 2013-12-10
      • 1970-01-01
      相关资源
      最近更新 更多