Android的 - 当达到年底的ListView加载更多项目 [英] Android - ListView to load more items when reached end

查看:150
本文介绍了Android的 - 当达到年底的ListView加载更多项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地实施了一个ListView其中每行是我创建的对象。 我有读取一些对象,并返回它们的数组,这是我传递给适配器构造函数的接口。 我想要做的是,因为列表可能会很长,我的接口允许读取每个一些更多的项目(0页,第1 ......)所以我想的时候,当用户到达列表的最后一行,加载下一个页面,并将其插入到ListView。 什么是这样做的正确方法? 其实我是想在到达清单结束触发的回调,我可以要求更多的页面,并将它们加载到列表中。 有点OT我的主要问题,但我不介意使用baseAdapter,既会工作,我只是不知道什么是对任务的更好。

I have successfully implemented a listView where each row is an object I've created. I have an interface which reads some objects and returns an array of them, which I pass to the adapter constructor. What I want to do is, since the list may be very long, My interface allows to read each time some more items (page 0, page 1...) so I Want, when the user reaches the last row of the list, load the next page and insert it to the listView. What is the proper way of doing so? I actually want the "reach end of list" trigger a callback where I can request for more pages and load them to the list. A bit OT to my main question, But I dont mind using a baseAdapter, both would work, I just dont know what's better for the task.

推荐答案

您可以在其中将有一个名为LoadMore按钮列表视图中添加脚注。下面是一个完整的教程的ListView具有负载更多按钮  或者你也可以实现onscrolllistener()和监听器添加到您的ListView

You can add a footer on the listView which will have a button named LoadMore. Here is a complete tutorial ListView with Load More Button Or you can implement onscrolllistener() and add this listener to your ListView

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if(!isLoading){
             isLoading = true;
             loadMoreData();
        }
    }
}

希望这将有助于

Hope it will help

这篇关于Android的 - 当达到年底的ListView加载更多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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