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

主Activity的Toolbar在Fragment中滚动失效及FAB不显示求助

Troubleshooting Your Toolbar Scroll and FAB Visibility Issues

Let's break down and fix both problems you're facing:


1. Toolbar Scroll Flags Only Working in One Fragment

The root cause here is a redundant layout behavior attribute in your Fragment's RecyclerView.

What's Wrong:

Your MainActivity's FrameLayout (the container for Fragments) already has app:layout_behavior="@string/appbar_scrolling_view_behavior"—this is the correct place for it, as it tells the CoordinatorLayout to link the container's scroll events to the AppBarLayout. Adding the same attribute to your Fragment's RecyclerView creates a conflict, preventing the scroll flags from working across other Fragments.

Fix Steps:

  • Remove app:layout_behavior="@string/appbar_scrolling_view_behavior" from the RecyclerView in your problem Fragment's layout.
  • For all other Fragments with scrollable views (RecyclerView, NestedScrollView), ensure they do not have this behavior attribute set.
  • Verify that scrollable views in every Fragment have nested scrolling enabled (it's default in newer support libraries, but you can explicitly add android:nestedScrollingEnabled="true" to be safe).

2. FAB Visible in Preview but Not in Runtime

There are a few likely culprits here, let's go through them one by one:

Possible Fix 1: Ensure the FAB is Drawn on Top

Even though the FAB is declared after the RecyclerView in your RelativeLayout, layout rendering quirks might cause it to be hidden behind the RecyclerView. Add explicit elevation to force it to the foreground:

<android.support.design.widget.FloatingActionButton
    ...
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />

Possible Fix 2: Adjust Positioning Relative to Bottom Navigation

Your FrameLayout is set to android:layout_above="@+id/bottom_navigation", so the Fragment's layout doesn't include the area occupied by the bottom nav. The FAB's current bottom margin might not be enough to position it above the nav bar. Update the margin to account for the nav's height:

android:layout_marginBottom="61dp" <!-- 56dp (nav height) + 5dp existing margin -->

Possible Fix 3: Switch to CoordinatorLayout for the Fragment

Using CoordinatorLayout as the root of your Fragment's layout can resolve layering issues and gives you more control over the FAB's behavior. Here's a revised layout:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/feed_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        android:layout_margin="16dp"
        app:layout_gravity="bottom|end"
        android:src="@drawable/ic_search_black_24dp" />

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

Possible Fix 4: Check for Programmatic Changes

Double-check your Fragment's Java code to ensure you're not accidentally setting the FAB to View.GONE or View.INVISIBLE anywhere (e.g., in onCreateView or scroll listeners).


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:22:15