【问题标题】:RecycelerView loads all items at onceRecyclerView 一次加载所有项目
【发布时间】:2021-09-09 20:45:35
【问题描述】:

我有一个 recyclerview,当片段启动时,recyclerview 会立即加载所有项目,并且由于在此 recyclerview 上启用了分页,我需要它来加载正在显示的项目。当我为这个 recyclerview 设置一个固定的高度时,它可以正常工作。但我需要包装内容!这个布局有问题吗?

    <?xml version="1.0" encoding="utf-8"?>
<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">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
           app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/similarProductsRv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="8dp"
                android:layout_gravity="top"
                android:layout_marginRight="8dp"
                android:clipToPadding="false"
                android:paddingBottom="56dp" />

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

 
            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>
        
    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android xml android-recyclerview


    【解决方案1】:

    如果您使用 RecyclerView 滚动回调进行分页,那么您应该从 NestedScrollView 中取出您的 Recyclerview。由于 RecyclerView 在 NestedScrollView 内部,因此 RecyclerView 不会回收视图,而是会创建所有视图。这将导致 RecyclerView 调用下一页,如果您加载太多项目,也会导致严重的性能问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2021-06-18
      • 2021-10-08
      • 2017-03-05
      • 2017-06-21
      • 2017-11-08
      相关资源
      最近更新 更多