Android 是否在RecyclerView滚动条上平滑隐藏工具栏?

Android 是否在RecyclerView滚动条上平滑隐藏工具栏?,android,android-toolbar,onscrolllistener,Android,Android Toolbar,Onscrolllistener,目前我有一个RecyclerView,其中包含一些项目列表。我正在收听RecyclerView的滚动监听器,如果RecyclerView在某个点显示为500,它应该隐藏工具栏,当它跨越到500+时应该保持隐藏状态。类似地,当我到达时,它显示了工具栏,我也在搜索相同的解决方案,并找到了这个。 和我一起工作很好 隐藏工具栏的步骤 mToolbar.animate().translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateI

目前我有一个
RecyclerView
,其中包含一些项目列表。我正在收听RecyclerView的
滚动监听器
,如果RecyclerView在某个点显示为500,它应该隐藏工具栏,当它跨越到500+时应该保持隐藏状态。类似地,当我到达时,它显示了工具栏,我也在搜索相同的解决方案,并找到了这个。 和我一起工作很好

隐藏工具栏的步骤

mToolbar.animate().translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
要显示工具栏,请执行以下操作:

mToolbar.animate().translationY(mToolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();
在recycler视图的滚动侦听器上调用这些行

现在,由于侦听器为您提供工具栏的dx和dy值。 因此,在上述代码行中,您可以编写以下代码,而不是mToolbar.getTop()

int heightDelta += dy;
    bothToolbarLayouts.animate().translationY(-heightDelta).setInterpolator(new AccelerateInterpolator()).start();
瞧,你完了


或者,为了更好地理解它,请遵循

使用CoordinatorLayout而不是线性/相对布局,并将以下属性添加到工具栏

app:layout\u scrollFlags=“scroll | enterally”

CoordinatorLayout通过在用户向下滚动时隐藏工具栏并在用户向上滚动时再次显示来处理工具栏的可见性

代码:

<?xml version="1.0" encoding="utf-8"?>

<!-- $Id$ -->

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"  />

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

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


您想要淡入淡出效果还是只需向下滑动/滑动?
<?xml version="1.0" encoding="utf-8"?>

<!-- $Id$ -->

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"  />

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

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