当用户滚动到recyclerView时,我有一些麻烦试图隐藏工具栏。
工具栏是透明的,位于recyclerView上(通过FrameLayout)。我搜索了很多,但我没有找到解决这个错误行为的任何解决方案。
目前,我有这个xml:
<android.support.design.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"
android:orientation="vertical"
app:statusBarBackground="@android:color/transparent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar_activity" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
使用此代码,工具栏固定在顶部,并且不受app:layout_behavior =“@ string / appbar_scrolling_view_behavior”的影响。我已经尝试将该属性移动到FrameLayout,但在这种情况下,recyclerview位于工具栏下方,而不是它后面。
知道怎么解决这个问题?我要疯了...
设置属性
app:layout_scrollFlags="scroll|enterAlways"
到android.support.design.widget.AppBarLayout的子视图
创建一个自定义类,然后扩展RecyclerView.OnScrollListener
public class ScrollListener extends RecyclerView.OnScrollListener {
public ScrollListener() {
}
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
System.out.println("The RecyclerView is not scrolling");
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
System.out.println("Scrolling now");
break;
case RecyclerView.SCROLL_STATE_SETTLING:
System.out.println("Scroll Settling");
break;
}
}
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
//scrolling downwards: hide/show toolbar
System.out.println("Scrolled Downwards");
} else if (dy < 0) {
//scrolling downwards: hide/show toolbar
}
}
}
将侦听器附加到回收器视图
mRecyclerView.addOnScrollListener(new ScrollListener());
将这些滚动标记添加到应用栏布局的子项
app:layout_scrollFlags="scroll|enterAlways|snap"