gpt4 book ai didi

android - 如何使RecyclerView在NestedScrollView中进行回收?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:39 32 4
gpt4 key购买 nike

我尝试将RecyclerView等几种 View 放入NestedScrollView中。我使用了setNestedScrollingEnabled(false),它对于小型数据集看起来不错,但对于大型数据集却开始变得迟钝。

在花了一些时间记录onCreateViewHolder()方法后,我了解到回收者 View 会像旧ListView一样一次创建它们。我试图在RecyclerView文档中找到这种行为的原因,但是我在ScrollView description中找到了:

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.



我希望 NestedScrollView可以解决此问题,但看来并非如此。

有什么方法,例如,使用一些自定义的 LayoutManager来使用 RecyclerView及其 View 回收的优化?

P.s.当然,我知道方法 getItemViewType(int pos)以及使用此技术添加自定义页眉和页脚的可能性,但对我来说这似乎是一个丑陋的解决方法。是的,我目前正在使用它,因为拥有比这样大的性能问题更难维护的代码更好。

最佳答案

您需要像下面那样更改布局,您使用的是NestedScrollView,而不是您需要在android:nestedScrollingEnabled中添加RecyclerView属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
android:id="@+id/YOUR_NEESTED_SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/YOUR_RECYCLVIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:nestedScrollingEnabled="false" />

</android.support.v4.widget.NestedScrollView>
</LinearLayout>

关于android - 如何使RecyclerView在NestedScrollView中进行回收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782786/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com