android– 如何让RecyclerView在NestedScrollView中做回收?

android– 如何让RecyclerView在NestedScrollView中做回收?,第1张

概述我试图将几个视图(包括RecyclerView)放入NestedScrollView中.我使用了setNestedScrollingEnabled(false),它看起来很适合小数据集,但对于较大的数据集开始变得迟钝.在花了一些时间记录onCreateViewHolder()方法之后,我明白了recycleler视图会一次创建它们作为旧的ListView.我试图

我试图将几个视图(包括RecyclerVIEw)放入nestedScrollVIEw中.我使用了setnestedScrollingEnabled(false),它看起来很适合小数据集,但对于较大的数据集开始变得迟钝.

在花了一些时间记录onCreateVIEwHolder()方法之后,我明白了recycleler视图会一次创建它们作为旧的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优化视图回收

附:当然,我知道方法getItemVIEwType(int pos)以及使用这种技术添加自定义页眉和页脚的可能性,但对我来说它看起来像一个丑陋的解决方法.是的,我现在正在使用它,因为拥有比这么大的性能问题更难维护的代码更好.

解决方法:

您需要更改您的布局,如下所示,您使用的是nestedScrollVIEw,而不是需要在RecyclerVIEw中添加androID:nestedScrollingEnabled属性

<?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中做回收?全部内容,希望文章能够帮你解决android – 如何让RecyclerView在NestedScrollView中做回收?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1117382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存