【问题标题】:CollapsingToolbarLayout not Collapsing with RecyclerView Scroll BehaviorCollapsingToolbarLayout 不与 RecyclerView 滚动行为折叠
【发布时间】:2020-06-13 01:32:38
【问题描述】:

我有一个显示图像的 CollapsingToolbarLayout(放置在 ConstraintLayout 内以保持 1:1 的比例)和一个附有 com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior 的 RecyclerView。

Image of Layout

图像为绿色,RecyclerView 为蓝色。

问题是当我在 RecyclerView 上向上滚动时,CollapsingToolbarLayout 不会折叠。相反,RecyclerView 只是在整个布局下方滚动。我必须在 CollapsingToolbarLayout 内手动滚动才能折叠。

这是布局的代码:

<androidx.coordinatorlayout.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:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:src="@drawable/ic_launcher_background"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintDimensionRatio="1:1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我最初将 RecyclerView 放在 NestedScrollView 中,并将滚动行为附加到 NestedScrollView,但这会导致适配器为 RecyclerView 中的每个项目创建 ViewHolder 对象时出现问题,这会导致很多性能和滞后/冻结问题。

据我了解,出于这个原因,不应将 RecyclerView 放在 NestedScrollView 中,这就是我将 NestedScrollView 取出的原因。

任何帮助将不胜感激!

【问题讨论】:

    标签: android-recyclerview scroll android-coordinatorlayout android-collapsingtoolbarlayout android-nestedscrollview


    【解决方案1】:

    您应该在回收站视图中禁用嵌套滚动

    android:nestedScrollingEnabled="false"
    

    此外,如果您有嵌套的回收器视图,您应该在子回收器视图中禁用嵌套滚动。

    【讨论】:

    • 如果你有嵌套的 recyclerviews 并且孩子“偷”了触摸事件(我做了),这很有效,所以谢谢
    【解决方案2】:

    如果您希望图像在 recyclerview 滚动时折叠,请使用此滚动标志

    app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed"
    

    并使用

    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    

    在您的 recyclerview 中,android:nestedScrollingEnabled 属性不是您想要实现的目标。

    【讨论】:

      【解决方案3】:

      你看过https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example

      还可以查看您缺少的一些内容

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-06
        • 2015-09-14
        • 2019-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多