简体   繁体   English

如果适配器具有2000个以上的对象,则RecyclerView的性能

[英]RecyclerView performance if the adapter has more than 2000 objects

I have created a list via RecyclerView with more than 2000 items. 我已经通过RecyclerView创建了一个包含2000多个项目的列表。 RecyclerView will recycle controls but the list assigned has more than 2000 objects in it. RecyclerView将回收控件,但是分配的列表中有2000多个对象。 List might have even 4000 objects as well. 列表甚至可以包含4000个对象。

Is it a good idea to have all the objects in the list in memory or there is any other way to manage? 将列表中的所有对象都存储在内存中是个好主意,还是有其他管理方法?

Now if I implement endless scrolling or show loader to load data, it will be added to the list and notifyDataSetChanged(); 现在,如果我实现了无限滚动或显示加载程序以加载数据,它将被添加到列表和notifyDataSetChanged();中。 will be called. 将被称为。 But the question remains same to maintain a list of more than 2000 objects. 但是,要维护一个包含2000多个对象的列表,问题仍然存在。

Suggest better approach for this scenario. 为这种情况建议更好的方法。

If u initially have 2000 items then by any mean the RecyclerView will keep them in memory so that it can load them. 如果您最初有2000件物品,那么RecyclerView会以任何方式将它们保留在内存中,以便可以加载它们。 But if ur getting these item from network then as u said u can implement infinite scroll, load the item from ur web service as needed and add them to ur list. 但是,如果您是从网络上获取这些项目的,则可以按照您所说的那样实现无限滚动,请根据需要从您的Web服务加载该项目并将其添加到您的列表中。

i am currently going through same scenario but my data set is way big it has currently 2600 items but i have prepared my list to add nearly 100k items in it, the way i am dealing with this is that: 我目前正在经历相同的情况,但是我的数据集很大,它目前有2600个项目,但是我已经准备好在列表中添加近10万个项目,我正在处理的方式是:

I am using .json file in res/raw for my app with the items and loading all items in a List, Ok. 我正在为我的应用程序在res/raw使用.json文件,并将这些项目加载到列表中,确定。 Now instead of throwing this List with all items directly to the adapter what i opted to do is to have another List in which i will add items little by little and populate RecyclerView using load more mechanism. 现在,我没有选择将包含所有项目的列表直接扔到适配器上,而是选择了另一个List,在其中我将一点一点地添加项目并使用load more机制填充RecyclerView。 And when you have loaded whole items in RV simply clear the previously fetched list to clear memory. 当您在RV中加载了整个项目后,只需清除先前获取的列表即可清除内存。

Many people suggest to use Web Service or API and fetch data repeatedly which is, imo, a bogus approach, because if your app, suppose has 10,00,0000 users and at a time 450,000 user are online and they all doing fetch via load more what will it do to WebServer and above all all requests will eventually fetch data from DB (so many hits to DB.... is always considered as worst approach) so if going this way you also need to take care of API mechanism like implementing middleware data request look up to avoid taking users' request to DB every time and return them data from Web Server layer if the request of user is already made and is cached in this layer (definitely, difficult task but this is how it is being used these days). 许多人建议使用Web ServiceAPI并重复获取数据,这是imo的一种虚假方法,因为如果您的应用程序假设有10,00,0000个用户,而一次有450,000个用户处于在线状态,那么他们都通过负载进行获取它将对WebServer产生什么影响,最重要的是最终所有请求将最终从DB中获取数据(对DB的访问量如此之多。...始终被认为是最糟糕的方法),因此如果采用这种方式,则还需要注意API机制,例如实施中间件数据请求查询以避免每次将用户的请求带到DB时,如果已经发出用户请求并将其缓存在该层中,则从Web服务器层返回他们的数据(这无疑是一项艰巨的任务,但这就是这样做的方式)这些天用过)。

If you(or anyone) finds better approach kindly discuss. 如果您(或任何人)发现更好的方法,请进行讨论。 Hope i helped somehow. 希望我有所帮助。

If the object is too heavy/ or you fetching it from server, then better to manage virtual paging. 如果对象太重//或从服务器获取对象,则最好管理虚拟分页。 Let define your page size is 100, in onBind put a check that if it has reached 70%(showing 70th row) of your list then it will make a call to fetch another 100. 让我们定义您的页面大小为100,在onBind中放入一个检查,确认它是否已达到列表的70%(显示第70行),那么它将调用另一个100。

And don't worry about RecyclerView/ListView, they have no problem with the list size, because the maximum number of rows created by RecyclerView or ListView is the maximum rows you can view on the page whatever the size of your list is. 不用担心RecyclerView / ListView,它们的列表大小没有问题,因为无论列表的大小如何,RecyclerView或ListView创建的最大行数就是您可以在页面上查看的最大行数。

Hope it will help.. :) 希望它会有所帮助.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM