【问题标题】:Adjusting CoordinatorLayout behavior调整 CoordinatorLayout 行为
【发布时间】:2018-03-01 18:30:44
【问题描述】:

假设我们有以下布局:

<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

在这个布局中,如果我向上拖动SearchView,它会折叠并消失。但我想知道如何将这种拖动效果附加到我的RecyclerView。意味着我喜欢在向上滚动 RecyclerView 项目时折叠 SearchView,但在向下滚动 RecylcerView 项目时展开 SearchView。我是否需要为SearchView 创建自定义行为,或者可以通过可用行为创建此效果?

最好的问候

【问题讨论】:

    标签: android android-coordinatorlayout


    【解决方案1】:

    使用折叠式工具栏布局并将searchView包裹在其中...并用包裹recyclerView nestedScrollView 并将app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt; 添加到嵌套滚动视图中

    不要忘记将scrollFlagscontent_scrim 添加到您的折叠工具栏布局中......

    折叠工具栏布局应该在appBarLayout

    参考看看这个

    https://antonioleiva.com/collapsing-toolbar-layout/

    【讨论】:

    • 似乎不需要CollapsingToolbar。我只添加了NestScrollView 获得了所需的效果。
    • 你可以做两种方式@Afshin
    【解决方案2】:

    您需要做的唯一更改是将|enterAlways 添加到您现有的app:layout_scrollFlags 属性中。

    【讨论】:

    • 你的意思是在添加NestedScrollView之后,不是吗?因为似乎没有它,|enterAlways 是不够的。
    • @Afshin 不,我没有。 RecyclerView 是用于 AppBarLayout 的全功能滚动兄弟。
    • @Afshin 除非您在发布的代码中遗漏了一些内容。如果我复制/粘贴您发布的布局并将适配器 + 布局管理器附加到 RecyclerView,我会得到正确的折叠行为,而无需(进一步)修改。
    • 这很奇怪。它不适合我。但我应该说我没有分配了适配器+布局管理器,因为我只是在处理布局本身,我认为我不需要分配这两个来获得折叠行为。分配这两个后我会测试它。
    猜你喜欢
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2017-03-22
    相关资源
    最近更新 更多