简体   繁体   中英

Collapsing Toolbar not collapsing with RecyclerView

I'm trying to make the below code work but the toolbar doesn't collapse when using a recyclerView; however, it does't collapse when I surround the recyclerView with a NestedScrollView. Is there something I should change to avoid having to add the NestedScrollView?

<?xml version="1.0" encoding="utf-8"?>
<layout
    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"
    >
  <androidx.coordinatorlayout.widget.CoordinatorLayout
      android:id="@+id/coordinator"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView_issue_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/issue_item"
        />
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        >
      <com.google.android.material.appbar.CollapsingToolbarLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:contentScrim="@color/colorPrimary"
          app:expandedTitleGravity="top"
          app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
          >
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:title="All Issues"
            />
      </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
  </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

In my case, it was because I mistakenly had android:nestedScrollingEnabled="false" in my XML for the Recycler View. Maybe you had it set programmatically?

After removing that line, it works as expected.

对于我的情况,结果证明我需要设置recycler.nestedScrollingEnabled = true不是在这个顶级回收器上,而是在一个位于顶级回收器内膨胀的视图内的回收器上。

Change your RecyclerView's layout height to match_parent as @BenP. said and move your RecyclerView out of AppBarLayout. So, it will be:

<?xml version="1.0" encoding="utf-8"?>
    <layout
    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"
        >
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            >
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="@color/colorPrimary"
                app:expandedTitleGravity="top"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                >

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:title="All Issues"
                    />
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView_issue_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:listitem="@layout/issue_item"
            />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </layout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM