首页 > 解决方案 > CoordintatorLayout - AppBar 和 Layout 中的可滚动内容

问题描述

我正在创建一个基于CoordinatorLayout. 视图包含两个元素:ViewinCollapsingToolbarLayoutRecyclerViewas a content。整个内容也包含在内SwipleRefreshLayout- 向上滚动存在问题,但很容易修复(通过OnOffsetChangedListenerin AppBar

我的问题是 CollapsingToolbarLayout 中的视图可能高于屏幕(非常罕见,但可能),因此它必须是可滚动的。

看这个例子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

            <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <View android:id="@+id/very_big_height"
                      android:layout_width="match_parent"
                      android:background="@color/design_default_color_primary"
                      android:layout_height="2000dp"/>

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

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

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

当我通过 AppBar 滚动时,我可以将我的布局设置为这样的状态: 设备上的意外行为示例

蓝色区域有 id very_big_height,而白色区域实际上是屏幕上的一个空白区域,因为我的 RecyclerView 有一个wrap_content高度,里面没有项目。

我仍然可以通过蓝色区域滚动 AppBar,甚至完全滚动到屏幕外。如果我过度滚动它,就无法再次看到它。

我的预期行为是: AppBar 应该只滚动到屏幕底部。如果下面没有内容,它不应该隐藏。 此外,如果内容适合屏幕 AppBar 不应该是可滚动的(我已经建立了在没有内容时禁用滚动的解决方案,但当 AppBar 不适合屏幕时不起作用。

谢谢你的帮助!

标签: androidandroid-recyclerviewandroid-coordinatorlayoutandroid-collapsingtoolbarlayoutandroid-appbarlayout

解决方案


第一个 CoordinatorLayout 不能向 SwipeRefreshLayout 发送滚动事件。您可以尝试ChildCoordinatorLayout中的代码

其次是你可以设置 RecyclerView 匹配父级。


推荐阅读