滚动时,RecyclerView上的透明工具栏未隐藏

时间:2019-02-22 12:31:30

标签: android android-recyclerview android-coordinatorlayout android-appbarlayout

当用户在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在工具栏下方,而不是在工具栏下方。

关于如何解决此问题的任何想法?我要疯了...

3 个答案:

答案 0 :(得分:0)

设置属性
app:layout_scrollFlags="scroll|enterAlways"
到android.support.design.widget.AppBarLayout的子视图

答案 1 :(得分:0)

  

创建一个自定义类,然后扩展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());

答案 2 :(得分:0)

将这些滚动标志添加到您的应用栏布局的子项

app:layout_scrollFlags="scroll|enterAlways|snap"

相关问题