滚动时的Android工具栏提升

标签 android android-layout android-recyclerview android-toolbar

我尝试在 google maps android 应用中实现搜索栏:

enter image description here

当回收器 View 处于其初始状态时,工具栏没有高度。只有当用户开始滚动时,海拔才可见。并且搜索栏(工具栏)永远不会折叠。这是我试图复制的内容:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="64dp">

            <!-- content -->

        </android.support.v7.widget.Toolbar>

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

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

在这里你可以看到结果:

enter image description here

所以我的解决方案的问题是,工具栏的高度总是可见的。但我希望它仅在回收器 View 滚动到它后面时出现。设计支持库中是否有任何东西可以实现在谷歌地图应用中看到的这种行为?

我正在使用

com.android.support:appcompat-v7:23.2.0
com.android.support:design:23.2.0

最佳答案

接受的答案已过时。现在有内置功能可以做到这一点。我正在粘贴整个布局代码,以便您理解。

您只需将 CoordinatorLayoutAppBarLayout 一起使用。这种设计模式称为 Lift On Scroll,可以通过在 AppBarLayout 上设置 app:liftOnScroll="true" 来实现。

注意:liftOnScroll 属性要求您将 @string/appbar_scrolling_view_behavior layout_behavior 应用于 ScrollView (例如,NestedScrollViewRecyclerView等)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/default_background">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/default_background" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

引用本文档 https://github.com/material-components/material-components-android/blob/master/docs/components/AppBarLayout.md

关于滚动时的Android工具栏提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35724308/

相关文章:

java - 使用 libffmpeg 时如何计算 AVPicture.data[x] 指针 (YUV420p) 的大小

android - 编舞跳帧了!多少算太多?

java - 从另一个 Fragment 重置 RecyclerView 适配器

android - 如何向回收站 View 的所有单元格添加填充

android - 将 View 定位在两个 View (或最低 View )下

android - RecyclerView 在底部加载更多进度条

java - Android中的 Material 圆形按钮?

android - 是否可以将 EditTextPreference 与 CheckBoxPreference 结合使用?

android - Android 的布局。流体布局和固定布局

java - 使用 <include> android 时出现 NullPointerException