You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView向上滚动时隐藏BottomNavigationView,系统滚动行为失效如何解决

问题根因

你使用的HideBottomViewOnScrollBehavior滚动行为依赖CoordinatorLayout布局才能生效,当前根布局为RelativeLayout,是属性不生效的核心原因。以下是完整实现方案:


实现步骤

1. 替换根布局为CoordinatorLayout

修改activity_main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".MainActivity">

    <include
        android:id="@+id/fragment_container"
        layout="@layout/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        android:id="@+id/bottom_navigation_view"
        layout="@layout/bottom_navigation_view" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

2. 调整底部导航布局属性

修改bottom_nav_view.xml,将RelativeLayout专属的android:layout_alignParentBottom="true"替换为CoordinatorLayout适配的属性,完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.bottomnavigation.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_navigation_view_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginStart="30dp"
    android:layout_marginEnd="30dp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/bottom_navigation_style"
    android:elevation="5dp"
    app:itemIconSize="30dp"
    app:itemIconTint="@color/white"
    app:itemRippleColor="@android:color/transparent"
    app:labelVisibilityMode="unlabeled"
    app:layout_behavior="com.example.yourpackage.AutoShowBottomNavBehavior"
    app:menu="@menu/bottom_navigation">

</com.google.android.material.bottomnavigation.BottomNavigationView>

注意将com.example.yourpackage替换为你项目的实际包名。

3. 自定义滚动行为实现停止滚动自动展示

系统默认的滚动行为仅支持上滑隐藏、下滑显示,要实现滚动停止后自动展示底部导航,需要自定义Behavior继承系统实现扩展逻辑,Kotlin代码如下:

class AutoShowBottomNavBehavior(context: Context, attrs: AttributeSet?) : HideBottomViewOnScrollBehavior<BottomNavigationView>(context, attrs) {
    // 滚动停止后延迟显示的时间,可自行调整
    private val showDelay = 800L
    private val handler = Handler(Looper.getMainLooper())
    private val showRunnable = Runnable {
        slideUp(child)
    }

    override fun onStopNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView, target: View, type: Int) {
        super.onStopNestedScroll(coordinatorLayout, child, target, type)
        handler.removeCallbacks(showRunnable)
        handler.postDelayed(showRunnable, showDelay)
    }

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
        handler.removeCallbacks(showRunnable)
        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
    }
}

Java实现逻辑完全一致,对应翻译代码即可。


效果说明

实现后默认自带和BottomSheet一致的平滑滑动动画,无需额外配置动画,所有Fragment内的RecyclerView默认支持嵌套滚动,可直接触发该行为。

内容的提问来源于stack exchange,提问作者Vasant Raval

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 18:06:00