在 ListView 上实现无限滚动 [英] Implement Endless scroll on ListView

查看:30
本文介绍了在 ListView 上实现无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android ListView 组件上实现无限滚动,当滚动到列表中的最后一个元素时,它将向列表视图的底部添加新项目.

I would like to implement endless scroll on an android ListView component, that will add new items to the bottom of the listview when scrolled to the last element in the list.

推荐答案

实现 onscrolllistener()

implement the onscrolllistener()

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();
        }
    }
}

当您实现并将此侦听器添加到您的列表视图时,这将检测列表视图的末尾,因为滚动位置位于列表末尾,只需获取更多数据即可.并且在加载数据期间,您需要一个标志来在滚动位置结束时立即加载数据.因此,如果数据正在加载,并且在此期间向上滚动然后向下滚动,则不会获得更多数据进行复制.

as you implement and add this listener to your listview this will detect for the end of listview as scroll position was at end of list just get more data. And during the loading data you need one flag to load data at once when the scroll position goes to end. So if data is loading and during that time you scroll up then scroll down then it will not get more data for dupplication.

这篇关于在 ListView 上实现无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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