【发布时间】:2020-06-13 01:32:38
【问题描述】:
我有一个显示图像的 CollapsingToolbarLayout(放置在 ConstraintLayout 内以保持 1:1 的比例)和一个附有 com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior 的 RecyclerView。
图像为绿色,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