使用RecyclerView的AppBarLayout可以在不让RecyclerView过度滚动的情况下工作吗?

时间:2016-12-09 12:19:15

标签: android android-recyclerview android-coordinatorlayout android-appbarlayout

我的CoordinatorLayout,AppBarLayout和RecyclerView布局如下所示。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa"
    tools:context="com.elyeproj.recycleranimation.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/cheese_2"
                app:layout_collapseMode="parallax" />

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

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#aaa"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

它可以正常工作,AppBarLayout可以收缩,因为RecyclerView滚动如下面的GIF所示。它不会滚动到第100行之外。

enter image description here

当列表很短时,它不可滚动,因为RecyclerView是wrap-content,如下所示

enter image description here

但是,当列表不是太短,但不长于页面高度时,滚动超出行&#34; 100&#34;,并显示一些空白区域,如下所示。

enter image description here

我的问题是,对于方案3,是否有可能有一种方法可以防止过度滚动,即仅在第100行发生scrowing,而AppBarLayout仍然可以部分显示在上面?

1 个答案:

答案 0 :(得分:1)

尝试使用RecyclerView height to match_parent。