android - CoordintatorLayout - AppBar 和 Layout 中的可滚动内容
问题描述
我正在创建一个基于CoordinatorLayout
. 视图包含两个元素:View
inCollapsingToolbarLayout
和RecyclerView
as a content。整个内容也包含在内SwipleRefreshLayout
- 向上滚动存在问题,但很容易修复(通过OnOffsetChangedListener
in 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 不适合屏幕时不起作用。
谢谢你的帮助!
解决方案
第一个 CoordinatorLayout 不能向 SwipeRefreshLayout 发送滚动事件。您可以尝试ChildCoordinatorLayout中的代码
其次是你可以设置 RecyclerView 匹配父级。
推荐阅读
- r - 使用 R 将文本文件转换为表格
- python - ASCII动画闪烁python
- powershell - 从 CSV 用户列表中获取所有日历子文件夹
- r - R中的Web抓取教育数据
- javascript - 我如何在 firebase 上更新我的 react 项目主机?
- c - 如何在 C 编程中的单个脚本中写入、读取和删除文件
- java - 我对回收站视图和碎片有疑问?
- r - 如何自动选择一个好的 ylim 来读取 R 中 ggplot2 中的 geom_labels
- python - 如何制作 selenium python 脚本以继续单击按钮?
- python - 安装 turbodbc 时遇到问题