当 RecyclerView 为空时阻止工具栏折叠

Block Toolbar from collapsing when RecyclerView is empty

我有 CollapsingToolbarLayout,它依赖于在 ViewPager 的 Fragment 中滚动 RecyclerView。问题是 ViewPager 太高并且超出了 CoordinatorLayout 的范围,独立于它是否具有 "match_parent" 或 "wrap_content" layout_height。因此,即使 RecyclerView 只有很少的项目,ViewPager 也会滚动。 另一个问题是我无法在 ViewPager 的片段中制作静态元素。我想让 FAB 漂浮在 RecyclerView 上,但 FAB 位于屏幕底部边界下方,直到我将其向上滚动。

这里是一些代码

主要布局:

<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
android:background="@android:color/white">

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="122dp"
        android:fitsSystemWindows="true"
        android:id="@+id/collapsingToolbar"
        app:expandedTitleMarginStart="56dp"
        app:layout_scrollFlags="scroll|enterAlways">

        <android.support.v7.widget.Toolbar
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:id="@+id/toolbar"
            app:layout_collapseMode="pin"/>

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

    <android.support.design.widget.TabLayout
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:id="@+id/tabLayout"
        android:background="@color/default_green"
        app:tabIndicatorColor="@color/white"/>

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

片段:

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

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


<android.support.design.widget.FloatingActionButton
    android:id="@+id/bookMark"
    android:fitsSystemWindows="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_shape3x"
    android:layout_gravity="end|bottom"
    app:fabSize="normal"
    app:backgroundTint="@color/default_green"
    android:layout_margin="16dp"
    app:elevation="4sp"
    app:borderWidth="0dp"
    android:contentDescription=""/>
</android.support.design.widget.CoordinatorLayout>

我设法通过这种方式解决了这个问题。首先,我像解释的那样阻止了 AppBarLayout 的滚动 。 然后我将 FAB 直接放置到 CoordinatorLayout 并将它隐藏到 ViewPager 滚动条,如 。 我还想要一个 EditText 漂浮在 ViewPager 内的其中一个片段上。我把它放在 CoordinatorLayout 里面,让它在 ViewPager 滚动时上下滑动。