Android - ListView 在到达结束时加载更多项目 [英] Android - ListView to load more items when reached end

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

问题描述

我已经成功实现了一个 listView,其中每一行都是我创建的一个对象.我有一个接口,它读取一些对象并返回它们的数组,我将其传递给适配器构造函数.我想要做的是,由于列表可能很长,我的界面允许每次读取更多项目(第 0 页、第 1 页...)所以我想要,当用户到达列表的最后一行时,加载下一页并将其插入到 listView.这样做的正确方法是什么?我实际上希望到达列表末尾"触发回调,我可以在其中请求更多页面并将它们加载到列表中.我的主要问题有点过时,但我不介意使用 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.

推荐答案

您可以在 listView 上添加一个页脚,该页脚将有一个名为 LoadMore 的按钮.这是一个完整的教程 ListView with Load More Button或者您可以实现 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();
        }
    }
}

希望能帮到你

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

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