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



我有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中。当ViewPager滚动时,我把它放在CoordinatorLayout中,并使它上下滑动。

最新更新