Android - ListView 在到达结束时加载更多项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20458475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - ListView to load more items when reached end
提问by buddy123
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.
我已经成功实现了一个 listView,其中每一行都是我创建的一个对象。我有一个接口,它读取一些对象并返回它们的数组,我将其传递给适配器构造函数。我想要做的是,由于列表可能很长,我的界面允许每次读取更多项目(第 0 页、第 1 页...)所以我想要,当用户到达列表的最后一行时,加载下一页并将其插入到 listView。这样做的正确方法是什么?我实际上希望“到达列表末尾”触发回调,我可以在其中请求更多页面并将它们加载到列表中。我的主要问题有点过时,但我不介意使用 baseAdapter,两者都可以,我只是不知道什么对任务更好。
回答by Md. Monsur Hossain Tonmoy
You can add a footer on the listView which will have a button named LoadMore. Here is a complete tutorial ListView with Load More ButtonOr you can implement onscrolllistener() and add this listener to your ListView
您可以在 listView 上添加一个页脚,该页脚将有一个名为 LoadMore 的按钮。这是一个带有加载更多按钮的完整教程ListView或者您可以实现 onscrolllistener() 并将此侦听器添加到您的 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
希望它会有所帮助