检测ListView何时到达底部-onScroll()或onScrollStateChanged()? [英] Detect when ListView has reached the bottom - onScroll() or onScrollStateChanged()?

查看:91
本文介绍了检测ListView何时到达底部-onScroll()或onScrollStateChanged()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎已经有很多答案了;但是,我找不到通用的方法。
当到达底部时,我试图将数据添加到 ListView ;使用 AsyncTask 从Internet检索数据。 ListView 已经附加了一个适配器。

This seems a question with many answers already; however, I can't find a common approach to this. I'm trying to add data to a ListView when the bottom is reached; data are retrieved from Internet using an AsyncTask. ListView already has an adapter attached to it.

因此,我正在寻找一种方法来实现这一目标,

So, searching for a good way to accomplish this, I arrived at two different approaches.


  1. 第一个方法是 onScrollStateChanged()方法,基本上与页面有关。但是,它使用的参数在实际的API中没有对应关系。同时,链接使用正确的API,但我不知道是否正确。我尝试了两个链接中的第一个,但这有点麻烦。调试应用程序时, diff 的值变化很大,我不知道如何正确插入表达式。另外,我不知道如何确定一个偏移量,我可以从该偏移量开始检索数据;我的意思是,我不想在到达底部时执行代码,而是在到达之前执行代码。此外,有时即使我们滚动到顶部也会调用它。

  2. 第二个方法是 onScroll()方法,即在答案中使用,或者以其他方式在代码。我尝试改编这两个代码中的最后一个,但即使没有达到列表的底部,也会导致很多问题,并且数据也已加载。

  1. The first one, the onScrollStateChanged() approach, is basically related to this page. However, it use parameters that don't have correspondence in the actual API. At the same time, this link use the right API, but I don't know if in the right way. I tried with the first of the two links, but it's kind of meh. Debugging the app, diff values vary a lot and I don't know how to correctly interpet the expression. Also, I don't know how to fix an offset from which I can start to retrieving data; I mean, I'd like to execute code not when I'm about to reach the bottom, but just before I reach it. Also, sometimes it get called even if we scroll to the top.
  2. The second one is the onScroll() approach, that is used in this answer or, in a different way, in this code. I tried adapting the last of the two codes, but it cause many problems and the data are loaded even if we don't reach the bottom of the list.

那么,哪种方法最好?什么时候以及为什么我应该选择一个?
在我的情况下,应该使用哪两个?

So, what approach is best? When and why should I prefer one or the other? Which of the two should I use in my case?

推荐答案

以下是我建议的第三种方法的代码,其中我在自己的项目中使用。我使用适配器的 getView 方法来检测何时到达列表末尾。

Here's some code for my suggested third approach, which I use in my own projects. I use the adapter's getView method to detect when the end of the list has been reached.

public View getView(int position, View convertView, ViewGroup parent) {
    // handle recycling/creating/initializing view
    if(reachedEndOfList(position)) loadMoreData();
    return convertView;
}

private boolean reachedEndOfList(int position) {
    // can check if close or exactly at the end
    return position == getSize() - 1;
}

private void loadMoreData() {
    // Perhaps set flag to indicate you're loading and check flag before proceeding with AsyncTask or whatever
}

这篇关于检测ListView何时到达底部-onScroll()或onScrollStateChanged()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆