【问题标题】:Showing and hiding BottomAppBar on scroll在滚动时显示和隐藏 BottomAppBar
【发布时间】:2018-05-16 19:47:10
【问题描述】:

我正在尝试实现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"/>

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

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

【问题讨论】:

  • 查看this的问题,这是针对BottomNavigationView的,但我认为它适用于你

标签: android android-layout material-components


【解决方案1】:

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

【讨论】:

  • 您还应该将内容(在本例中为您的 ConstraintLayout)放入 NestedScrollView。这就是允许 CoordinatorLayout 接收滚动事件并调度到将隐藏它的 BottomAppBar 行为的原因。
  • @Cameron,您能否分享代码片段以帮助我更好地理解?我在 Fragment 内的 ConstraintLayout 中有一个 RecyclerView,该站点位于 FrameLayout 中。在这里如何使用 NestedScrollView?
  • 您需要有一个CoordinatorLayout,其中RecyclerView(或实现NestedScrollingChild 的东西)和BottomAppBar 作为直接子级。然后您只需在BottomAppBar 上设置app:hideOnScroll 即可启用该功能。这有意义吗?
【解决方案2】:

HideOnScroll 不适用于 ScrollView,请改用 NestedScrollView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 2021-06-17
    • 2020-11-16
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2019-07-28
    相关资源
    最近更新 更多