我正在尝试使工具栏在滚动时折叠,当一个嵌套在片段中的 RecyclerView 被滚动时。首先,这是我的主要布局:
<DrawerLayout>
<RelativeLayout
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="5dp"
app:layout_scrollFlags="scroll|enterAlways"
>
</Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
<!-- ignore -->
<drawercontents>
</DrawerLayout>
所以你可能已经猜到我的片段被加载到@id/container
中。我的第一个片段包含了recyclerview,我在该recyclerview上设置了app:layout_behavior="@string/appbar_scrolling_view_behavior"
。这确实起作用,工具栏在滚动时折叠。问题是当它没有折叠时,工具栏覆盖了片段的顶部内容。将片段容器的顶部边距添加到工具栏大小相等只会在工具栏折叠时留下空白(显然)。
缺少什么?有什么想法吗?
编辑:根据要求,这里是包含recyclerview的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/feed"
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:background="#00000000"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>