我在将RecyclerView放在NestedScrollView中时遇到了问题,这会导致RecyclerView适配器的所有元素都被渲染出来。
这是一个相当大的问题,因为RecyclerView显示的列表可能包含数百个元素。
目前,这导致了相当多的延迟(显然),因为它必须一次性渲染所有视图,并且不能像RecyclerView通常那样重用任何已充气的视图。
这是我当前的XML(删除了一些冗余以将其最小化):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp">
<!-- Some content -->
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Some more content -->
</LinearLayout>
<!-- Product list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
这是我创建的Fragment中的onCreateView()方法,它会填充包含NestedScrollView和RecyclerView的视图:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.category_content_fragment, container, false);
ButterKnife.bind(this, root);
List<Product> products = new ArrayList<>(); //This is populated by other means, not relevant to the issue
productsRecyclerView.setNestedScrollingEnabled(false);
productsRecyclerView.setHasFixedSize(true);
productsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ProductsContentAdapter productsContentAdapter = new ProductsContentAdapter(products);
productsRecyclerView.setAdapter(productsContentAdapter);
return root;
}
我看到了这篇文章,与问题相关:
如何将RecyclerView放在NestedScrollView中? 但不幸的是,它没有提到最终解决方案。
澄清一下: RecyclerView可以完美滚动,也能在正确的时间显示,但问题是它立即渲染所有子项,可能包含多个元素,即使屏幕最多只显示5-6个。
如果需要更多信息,请随时提问。
------- 编辑 ------- 尝试了许多其他解决方案后,我最终使用了Jeeva Nandhan的解决方案。 在提问之前,我知道这是一个可能的解决方案,但我有11个不同的可能需要放入RecyclerView的视图,所以我想避免使用它。 使用不同的ViewTypes之后,它运行得非常完美。我担心由于高数量的ViewTypes会效率低下,但它非常流畅。