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

BottomNavigationBehavior滚动时底部导航栏不隐藏问题求助

Fix: Custom BottomNavigationBehavior Not Hiding on Scroll

Let's break down why your custom BottomNavigationBehavior isn't working and fix it step by step.

The Root Cause

The core issue here is that CoordinatorLayout's nested scroll event system can't penetrate the intermediate RelativeLayout. Your BottomNavigationView and scrollable ViewPager are both wrapped inside a RelativeLayout, but CoordinatorLayout's behavior mechanism requires the target view (your BottomNavigationView) to be a direct child of the CoordinatorLayout. When it's nested inside another layout, the scroll events from your RecyclerView never reach the behavior attached to the BottomNavigationView.

Solution (Without Breaking Your RelativeLayout)

Since you mentioned modifying the RelativeLayout causes crashes, we'll only adjust the layout hierarchy to move the BottomNavigationView out of the RelativeLayout while keeping the rest of your layout intact:

Updated Main Layout

<android.support.design.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="com.blipclap.creativegraphy.HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <!-- Keep your existing RelativeLayout with TabLayout and ViewPager -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_below="@+id/tabLayout"
            android:layout_above="@+id/navigation"></android.support.v4.view.ViewPager>

    </RelativeLayout>

    <!-- Move BottomNavigationView to be a direct child of CoordinatorLayout -->
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@android:color/background_dark"
        app:itemTextColor="@android:color/background_dark"
        app:menu="@menu/bottom_navigation_menu"
        app:layout_behavior=".Helper.BottomNavigationBehavior"></android.support.design.widget.BottomNavigationView>

</android.support.design.widget.CoordinatorLayout>

Minor Optimization to Your BottomNavigationBehavior

Your current code uses an unnecessary string conversion to get the view height. Let's clean that up to avoid potential issues:

@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
    // Replace the string conversion with a direct cast
    float maxTranslation = (float) child.getHeight();
    child.setTranslationY(Math.max(0f, Math.min(maxTranslation, child.getTranslationY() + dyConsumed)));
}

Additional Checks

  • Ensure your Fragment's RecyclerView has android:nestedScrollingEnabled="true" (it's enabled by default, but explicit setting won't hurt)
  • Double-check that the app:layout_behavior="@string/appbar_scrolling_view_behavior" is set on your RecyclerView (you already have this, which is good)

This change works because now the CoordinatorLayout directly manages the BottomNavigationView and can forward scroll events from the ViewPager's RecyclerView to your custom behavior, allowing the navigation bar to hide/show on scroll as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:04:16