简体   繁体   English

在列表视图中向下滚动时如何加载更多项目?

[英]How can I load more items when scrolling down in listview?

First I want to say: This question is no duplicate! 首先我想说:这个问题不可重复!

I already read a lot of questions about loading more items when scrolling down . 我已经阅读了很多有关向下滚动时加载更多项目的问题。

This was most helpful for me. 对我最有帮助。

But all questions I read aren't explaining the basic principle. 但是我读的所有问题都不能解释基本原理。

So, what I mean is: 所以,我的意思是:

My app gets data from json and displays it in a ListView , but it's not possible to load all items from database with one request . 我的应用程序从json获取数据并将其显示在ListView ,但是无法通过一个request从数据库加载所有项目。 The app crashes… 该应用程序崩溃了…

The solution is to load only 10 items and on scrolling down 10 items again. 解决方案是仅加载10个项目,然后再次向下滚动10个项目。

But what is the basic principle to do this? 但是,这样做的基本原理是什么?

I thought about these 2 different options: 我考虑了以下两种不同的选择:

  1. Set a LIMIT in my PHP file and send for each 10 items a new request from android and set LIMIT +10. 在我的PHP文件中设置一个LIMIT,并为每10个项目发送一个来自android的新请求,并设置LIMIT +10。
  2. Send one request from android and getting all data from json, but only displaying 10 items. 从android发送一个请求,并从json获取所有数据,但仅显示10个项目。

Code isn't necessary, because I want to know the principle of doing this. 不需要代码,因为我想知道这样做的原理。

Usually you would load 10 new items from the server each time. 通常,您每次都会从服务器加载10个新项目。 Use a page-parameter to identify which 10 items you need and where to place them. 使用页面参数来确定需要哪些10个项目以及将它们放置在何处。

loading all items at once could be way too expensive: The delay could be long and the user's data-plan won't be happy either. 一次加载所有项目可能太昂贵了:延迟可能会很长,并且用户的数据计划也不会令人满意。 Obviously depending on how many items there are and what contents they have. 显然取决于有多少项目以及它们具有什么内容。

You will have to find the trade-off. 您将必须找到权衡。 Based on your data size, sometimes it makes sense to parse and save it all in local database. 根据您的数据大小,有时解析所有这些并将其保存在本地数据库中是有意义的。

Just for 200-300 records, you don't want to make another api call after every 50 records in list. 仅针对200-300条记录,您不想在列表中每50条记录后再进行一次api调用。 Remember with mobile app user scrolls up and down very often. 记住,使用移动应用程序时,用户经常上下滚动。 You might be unnecessarily sending multiple requests to your server, which might be an overload(depending on user count). 您可能不必要向服务器发送多个请求,这可能会导致过载(取决于用户数量)。

If you go with option 2, you can make use of something like JobIntentService to silently fetch data and save locally. 如果使用选项2,则可以使用JobIntentService方法静默获取数据并保存在本地。

This approach will also let your user interact with no internet(offline mode) scenarios. 这种方法还可以让您的用户在没有Internet(脱机模式)的情况下进行交互。

The approach I use in my apps is: 我在应用程序中使用的方法是:

Step # 1: I load first set of data with limit from server and then store the last record data id in a variable. 步骤#1:我从服务器加载具有限制的第一组数据,然后将最后一条记录数据ID存储在变量中。

Step # 2: Implement the on scroll end listener to your listView or RecyclerView. 步骤#2:将滚动结束侦听器实现到listView或RecyclerView。

Step # 3: Start another request inside on scroll end listener and load new records.(Here I set again data id in a variable) 步骤#3: 在滚动结束侦听器中启动另一个请求并加载新记录。(在这里我再次在变量中设置数据ID)

For Example: 例如:

Start the request when the activity starts and do what I explained earlier. 活动开始时启动请求,然后执行我之前解释的操作。

GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId());

Then inside your on Scroll End Listener 然后在您的Scroll End Listener中

GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId()+"&limit=10&lastDataId="+BusinessItems[index].mBusinessId)

Edit To avoid duplicate api call inside GetBusinesses() check if the request was started previously, well the idea is create a boolean initially false and then in your GetBusinesses() function make it true before starting the request and once the data is loaded and request is finish make it false again. 编辑为了避免内部GetBusinesses重复API调用()检查请求先开始,以及这个想法是不是真的开始请求之前,一旦数据被加载并请求创建一个布尔值最初为false,然后在GetBusinesses()函数make完成后,再次使其为 I hope this help. 希望对您有所帮助。

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

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