滚动时的Android工具栏高度 [英] Android toolbar elevation when scrolling

查看:59
本文介绍了滚动时的Android工具栏高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Google Maps android应用中实现搜索栏:

I try to implement a search bar like in google maps android app:

当回收站"视图处于初始状态时,工具栏将没有高程.仅当用户开始滚动时,高程才可见.搜索栏(工具栏)永不崩溃.这是我尝试复制的内容:

When the recycler view is in its initial state, the toolbar has no elevation. Only when the users starts scrolling the elevation becomes visible. And the search bar (toolbar) never collapses. Here is what I tried to replicate this:

<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>

在这里您可以看到结果:

And here you can see the result:

所以我的解决方案的问题是,工具栏的高度始终可见.但我希望它仅在回收者"视图向后滚动时才显示.设计支持库中是否有任何东西可以实现google maps应用程序中所示的行为?

So the problem with my solution is, that the elevation of the toolbar is always visible. But I want it to appear only when the recycler view scrolls behind it. Is there anything from the design support library that enables such behavior as seen in the google maps app?

我正在使用

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

推荐答案

编辑如评论中所指出,我的答案现在已过时,请参阅

EDIT As pointed out in the comments, my answer is now outdated, see https://stackoverflow.com/a/58272283/4291272

是否使用 CoordinatorLayout ,就高度而言, RecyclerView.OnScrollListener 似乎是正确的方法.但是,根据我的经验, recyclerview.getChild(0).getTop()不可靠,应该用于确定滚动状态.相反,这是起作用的:

Whether you are using a CoordinatorLayout or not, a RecyclerView.OnScrollListener seems like the right way to go as far as the elevation is concerned. However, from my experience recyclerview.getChild(0).getTop() is not reliable and should not be used for determining the scrolling state. Instead, this is what's working:

private static final int SCROLL_DIRECTION_UP = -1;
// ...
// Put this into your RecyclerView.OnScrollListener > onScrolled() method
if (recyclerview.canScrollVertically(SCROLL_DIRECTION_UP)) {
   // Remove elevation
   toolbar.setElevation(0f);
} else {
   // Show elevation
   toolbar.setElevation(50f);
}

请确保将 LayoutManager 分配给 RecyclerView ,否则调用 canScrollVertically 可能会导致崩溃!

Be sure to assign a LayoutManager to your RecyclerView or the call of canScrollVertically may cause a crash!

这篇关于滚动时的Android工具栏高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆