Toolbar与RecyclerView重叠问题排查:已用CoordinatorLayout仍异常
Hey there! Let's sort out that frustrating overlap between your Toolbar and RecyclerView. After checking your layout code, I spotted the key issue and have a couple of practical solutions for you.
What's Causing the Overlap?
Your NestedScrollView isn't configured to respect the space occupied by the AppBarLayout (which holds your Toolbar). Without the right behavior attribute, it just renders directly under the Toolbar instead of adjusting its position to avoid overlap.
Solution 1: Fix the Existing Layout (With NestedScrollView)
Add the app:layout_behavior attribute to your NestedScrollView to link it with the AppBarLayout. Here's the corrected code:
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- AppBarLayout remains unchanged --> <android.support.design.widget.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:titleTextColor="@color/White" app:layout_scrollFlags="scroll|enterAlways|snap"> <com.ct.listrtrial.Custom.CustomTextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pulse" android:textColor="@color/White" android:textSize="30sp"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <!-- Added app:layout_behavior to NestedScrollView --> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout 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:orientation="vertical" tools:context="com.ct.listrtrial.fragments.FeedFragment"> <android.support.v7.widget.RecyclerView android:id="@+id/feed_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="15dp"> </android.support.v7.widget.RecyclerView> </LinearLayout> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_marginBottom="5dp" android:src="@drawable/ic_search_black_24dp" /> </android.support.design.widget.CoordinatorLayout>
Solution 2: More Efficient Layout (Remove NestedScrollView)
Since RecyclerView already supports nested scrolling natively, wrapping it in a NestedScrollView is unnecessary and can hurt performance (it disables RecyclerView's view recycling feature). Here's a cleaner, more efficient version:
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.design.widget.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:titleTextColor="@color/White" app:layout_scrollFlags="scroll|enterAlways|snap"> <com.ct.listrtrial.Custom.CustomTextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pulse" android:textColor="@color/White" android:textSize="30sp"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <!-- RecyclerView directly under CoordinatorLayout with behavior attribute --> <android.support.v7.widget.RecyclerView android:id="@+id/feed_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="15dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> </android.support.v7.widget.RecyclerView> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_marginBottom="5dp" android:src="@drawable/ic_search_black_24dp" /> </android.support.design.widget.CoordinatorLayout>
Key Changes Explained
app:layout_behavior="@string/appbar_scrolling_view_behavior": This tells the CoordinatorLayout that the view should coordinate with the AppBarLayout. It automatically adds padding to avoid overlapping the Toolbar and enables the scroll effects you set withscroll|enterAlways|snapon the Toolbar.- Removed
layout_weightfrom RecyclerView: When using CoordinatorLayout, we don't need weight—settinglayout_height="match_parent"works because the behavior handles spacing automatically.
内容的提问来源于stack exchange,提问作者user9550968

