首页 > 解决方案 > Fab Button 在 Fling 上使用 HideOnScroll 覆盖 BottomAppBar - 可能是一个错误

问题描述

我正在FabButton使用BottomAppBar. 布局由 a 组成,RecyclerView问题是当用户滚动滚动时RecyclerViewFabButton覆盖BottomAppBar并需要 3-10 秒才能自动重置。RecyclerView当滚动一点点或缓慢滚动时,不会发生此行为。但是,对于用户来说,这看起来像是一个错误的行为。看看它,看看是否有可能的解决方案。

我正在使用HideOnScroll属性 astrue并更改隐藏或出现的FabAlignment时间EndCenter时间。它工作正常,当我没有将它与隐藏时BottomAppBar对齐时,这个错误不会出现,但我想要那个。FabButtonBottomAppBar

你可以在这里观看问题:视频 - Imgur

  1. XML 布局

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include
            layout="@layout/sample_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:clickable="false"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:fabAlignmentMode="center"
            app:hideOnScroll="true"/>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fabButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            android:src="@drawable/ic_baseline_add_24"
            app:tint="@color/colorPrimary"
            app:layout_anchor="@id/bottomBar"/>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
  2. 科特林代码:

    recyclerView.setOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if (dy > 0) 
                bottomBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END
            else if (dy < 0)
                bottomBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
        }
    })
    

而且他们没有提供fabCradleVerticalOffset动态更改它的属性。

标签: androidkotlinfloating-action-button

解决方案


if (dy > 0) 
            bottomBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END
        else if (dy < 0)
            bottomBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
    

在这行代码中,当列表垂直滚动时,fab 图标将移动到末尾,如果没有滚动,它将移动到中心,所以我认为你应该更改这行代码或者只是没有它就可以工作。


推荐阅读