我尝试在 google maps android 应用中实现搜索栏:
当回收器 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>
在这里你可以看到结果:
所以我的解决方案的问题是,工具栏的高度总是可见的。但我希望它仅在回收器 View 滚动到它后面时出现。设计支持库中是否有任何东西可以实现在谷歌地图应用中看到的这种行为?
我正在使用
com.android.support:appcompat-v7:23.2.0
com.android.support:design:23.2.0
最佳答案
接受的答案已过时。现在有内置功能可以做到这一点。我正在粘贴整个布局代码,以便您理解。
您只需将 CoordinatorLayout
与 AppBarLayout
一起使用。这种设计模式称为 Lift On Scroll,可以通过在 AppBarLayout
上设置 app:liftOnScroll="true"
来实现。
注意:liftOnScroll
属性要求您将 @string/appbar_scrolling_view_behavior
layout_behavior 应用于 ScrollView (例如,NestedScrollView
、RecyclerView
等)。
<?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>
关于滚动时的Android工具栏提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35724308/