【发布时间】:2019-10-04 13:20:09
【问题描述】:
我正在创建一个基于CoordinatorLayout 的视图。该视图包含两个元素:View 中的 CollapsingToolbarLayout 和 RecyclerView 作为内容。整个内容也包含在 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