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

查看:13
本文介绍了检测 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() 方法,用于 this 答案或者,以不同的方式,在 this 代码中.我尝试修改两个代码中的最后一个,但它导致了很多问题,即使我们没有到达列表底部,数据也会被加载.
  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天全站免登陆