ListView / ListAdapter分页

时间:2010-04-11 20:33:40

标签: android

我正在尝试在自定义ListAdapter中实现分页。现在,我只是在ListView中的最后一项变得可见时发出下一页的请求,方法是getView() position >= ListAdapter.getCount()的大小{{1}} }。

它工作正常,但我想知道是否有更好的方式(或不同的方式)只有在列表中的最后一项实际上对用户可见时才会发出请求。有人知道吗?

3 个答案:

答案 0 :(得分:4)

我几乎以同样的方式这样做:

public static final int SCROLLING_OFFSET = 5;
// ...
private final ArrayList<T> items = new ArrayList<T>();
// ...
if (SCROLLING_OFFSET == items.size() - position) {
    if (hasNextPage()) {
        addNextPage();
    }
}

private boolean hasNextPage() {
    // basically calculates whether the last 2 pages contained the same # of items
}

private void addNextPage() {
    // show spinner
    // fetch next page in a background thread
    // add to items
    notifyDataSetChanged();
}

答案 1 :(得分:2)

我认为有更好的方法。实现OnScrollListener接口。看看这个:Endless Scrolling ListView

答案 2 :(得分:0)

尝试完全删除支票。根据我的经验,只有当条目即将出现在屏幕上时才会调用getView()。