工具栏下方的Android RecyclerView

use*_*408 36 android toolbar android-recyclerview

我有一个自定义的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>
Run Code Online (Sandbox Code Playgroud)

Hải*_*yễn 51

<?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>
Run Code Online (Sandbox Code Playgroud)

试试这个.我删除了ViewPager并向RecyclerView添加了滚动行为


war*_*ero 42

如果添加滚动行为无法解决您的问题

app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)

然后尝试这个

我必须给recyclelerview提供填充,这相当于toolbar/actionbar(app bar)的高度.

  android:paddingTop="?attr/actionBarSize"  
Run Code Online (Sandbox Code Playgroud)

将以上行添加到recyclerview xml文件中

  • 这个技巧有效但滚动视图滚动并且appbar布局被隐藏,然后填充看起来很奇怪. (3认同)
  • @SamikBandyopadhyay:也许添加`android:clipToPadding =“ false”`? (2认同)

Rav*_*ian 14

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

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)

错过上面的这行就会出现这个问题.


ram*_*mon 6

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

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)

IE:

<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" />
Run Code Online (Sandbox Code Playgroud)

  • 如果你在主布局中使用 `FrameLayout` 作为容器,并且有一个单独的布局包含 `RecyclerView` - 在这种情况下,你应该将 `app:layout_behavior=` 添加到主布局中的 `FrameLayout`! (3认同)