首页 > 解决方案 > 如何在 NestedScrollView 中放置 RecyclerView?

问题描述

我在 NestedScrollView 中有一个 RecyclerView。当 recyclerView 适配器列表大小较小时,它工作正常。但是当列表大小很大时,它会使我的应用程序崩溃(或造成更多延迟)。我该如何解决。代码如下。

XML:

   <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                 <com.google.android.material.textview.MaterialTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:text="0 Products"
                    android:textSize="16sp"
                    android:theme="@style/radio_font_textview" />

              <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/products_recycler"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="15dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>


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

爪哇:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ProductView.this);
productRecyclerAdapter = new ProductViewRecyclerAdapter(mData,mData2,ProductView.this);
products_recycler.setAdapter(productRecyclerAdapter);
ViewCompat.setNestedScrollingEnabled(products_recycler, false);
products_recycler.setLayoutManager(linearLayoutManager);

标签: androidandroid-recyclerviewscrollview

解决方案


回收器视图旨在解决与内存相关的问题并提高性能,因为它回收了屏幕外的视图持有者并轻松处理大量项目。

但是,当您将 aRecyclerView放入其中时,NestedScrollView它会失去回收ViewHolders 的能力,因此在将大量数据加载到其适配器中时会导致问题。

因此,要么减少要加载到回收器中的项目的大小/长度,要么提供适当的用例和崩溃堆栈跟踪,以便我们可以提出适当的解决方案


推荐阅读