Android 工具栏下方的 RecyclerView

Android RecyclerView below Toolbar

我有一个自定义 RecyclerView 和一个工具栏,向下滚动时隐藏,向上滚动时出现。我对 RecyclerView 的位置有疑问,它在工具栏下方,我使用了该行为但它似乎不起作用。

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<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="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />

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

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

我删除了 ViewPager 并向 RecyclerView 添加了滚动行为

如果添加滚动行为不能解决您的问题

app:layout_behavior="@string/appbar_scrolling_view_behavior"

然后试试这个

我必须给 recyclerview 填充等同于 toolbar/actionbar(应用栏)高度的填充。

  android:paddingTop="?attr/actionBarSize"  

将以上行添加到 recyclerview xml 文件

您需要将此属性添加到您的 RecyclerView:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

即:

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

当我从另一个布局中包含 RecyclerView 时,出现了同样的问题。我在 recyclerview 上添加了以下行,问题不再存在。

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

缺少上面这行会出现这个问题。

尝试在 ReyclerView 中添加这一行

  android:layout_marginTop="?attr/actionBarSize"