【问题标题】:CoordintatorLayout - scrollable content in both, AppBar and LayoutCoordintatorLayout - AppBar 和 Layout 中的可滚动内容
【发布时间】:2019-10-04 13:20:09
【问题描述】:

我正在创建一个基于CoordinatorLayout 的视图。该视图包含两个元素:View 中的 CollapsingToolbarLayoutRecyclerView 作为内容。整个内容也包含在 SwipleRefreshLayout 中 - 向上滚动时出现问题,但很容易修复(通过 AppBar 中的 OnOffsetChangedListener

我的问题是 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 不适合屏幕时不起作用。

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    滚动问题是由 SwipetoRefreshLayout 的父视图引起的,您的 xml 的根视图应该是 CoordinatorLayout

    我不知道使用 SwipetoRefreshLayout 的目的是什么,但是滚动元素过多可能会导致一些糟糕的滚动体验

    【讨论】:

    • 不是这个原因。没有 SwipeToRefresh 就无法工作,此外,这个协调器布局被这个布局包裹是有原因的(比如滑动刷新 posibilitty)
    • 我想指出,在我的情况下,滑动刷新是必须具备的功能。
    【解决方案2】:

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

    二是可以设置RecyclerView匹配父级。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2019-03-24
      相关资源
      最近更新 更多