【问题标题】:Transparent Toolbar over RecyclerView is not hidding when scrollingRecyclerView 上的透明工具栏在滚动时不隐藏
【发布时间】:2019-02-22 12:31:29
【问题描述】:

当用户滚动 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 位于工具栏下方,而不是其后面。

知道如何解决这个问题吗?我要疯了……

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 只要确保你添加了android设计依赖,为我工作implementation "com.android.support:design:28.0.0"
  • 当然,添加了依赖。您是否在工具栏后面显示recyclerview?或者recyclerview在工具栏下方?
  • 在我的情况下,recyclerview 应该在工具栏后面 =( 无论如何感谢您的帮助!
  • 我猜你想要视差效果,如果这样的话这个链接可能对你有帮助Parallax-Recycleview
【解决方案2】:

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

【讨论】:

  • 这样recyclerview就在Toolbar下面了。我希望回收站在工具栏后面
  • 请尝试CollapsingToolbarLayout
  • 是的,我也尝试过 collapsingToolbarLayout,但我也没有实现
【解决方案3】:

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

app:layout_scrollFlags="scroll|enterAlways|snap"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多