android - AppBarLayout with RecyclerView 可以在不让 RecyclerView 过度滚动的情况下工作吗?

标签 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

但是,当列表不是太短,但不超过页面高度时,滚动超出“100”行,并显示一些空白区域,如下所示。

enter image description here

我的问题是,对于场景 3,是否有可能有一种方法来防止过度滚动,即滚动只发生到第 100 行,并且 AppBarLayout 仍然部分可见,如上所示?

最佳答案

尝试将 RecyclerView 高度与父级匹配。

关于android - AppBarLayout with RecyclerView 可以在不让 RecyclerView 过度滚动的情况下工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060361/

相关文章:

android - 当 NavHostFragment 包含非 ScrollView 时,CoordinatorLayout 作为顶级装饰

android - CoordinatorLayout 行为是否只能应用于 CoordinatorLayout 的直接子项?

java - 如何注入(inject)单例?

android - 创建 AVD 时出现问题

android - 自定义 selectableItemBackground

Android RecyclerView.Adapter 允许在单个适配器中列出不同的对象类型?

android - 如何清除回收站 View 中突出显示的项目?

android - 带有 RecyclerView 和折叠标题的 CoordinatorLayout

c# - 如何绑定(bind)使用转换器格式化的文本?

java - 弱引用移除时的回调