Cal*_*Rai 5 scroll android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout android-nestedscrollview
我有一个显示图像的 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>
Run Code Online (Sandbox Code Playgroud)
我最初将 RecyclerView 放在 NestedScrollView 中,滚动行为附加到 NestedScrollView,但这导致适配器在为 RecyclerView 中的每个项目创建 ViewHolder 对象时出现问题,这导致了很多性能和滞后/冻结问题。
根据我的理解,出于这个原因,不应将 RecyclerView 放置在 NestedScrollView 中,这就是我将 NestedScrollView 取出的原因。
任何帮助,将不胜感激!
简而言之,您应该在回收器视图中禁用嵌套滚动
android:nestedScrollingEnabled="false"
Run Code Online (Sandbox Code Playgroud)
另外,如果您有嵌套的回收器视图,则应该在子回收器视图中禁用嵌套滚动。
| 归档时间: |
|
| 查看次数: |
445 次 |
| 最近记录: |