简体   繁体   中英

Android Dynamically load Listview at scroll end?

我正在从Http服务器加载一个listview,一次20个, 在Listview的末尾我想从服务器加载下一个20个数据 ,这个过程将继续,直到数据在服务器结束,我使用扩展BaseAdapter的类填充前20个数据。我该怎么办?

This looks to be an elegant solution as well: http://benjii.me/2010/08/endless-scrolling-listview-in-android/

It also implements an AbsListView.OnScrollListener , and uses an AsyncTask to load more content as soon as a certain threshold of remaining items in the current scroll view is reached.

好吧,我已经看到这是一个常见的问题,所以我已经创建了一个带有自定义加载的SMALL LIBRARY ,并在github中刷新ListView,请在此处查看 ,所有内容都在存储库中进行了解释。

You can implement an AbsListView.OnScrollListener , that gets informed and which allows you to load more data.

See eg

....
ListView.setOnScrollListener(this);
....

And then have a look at https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/TweetListActivity.java#L287

Or take a look at the List9 example from the sdk examples.

Heiko Rupp's code is the best option for this problem. The only thing you should do is implement onScrollListener and in onScroll() just check if there is an end of list by using the code

boolean loadMore =  firstVisibleItem + visibleItemCount >= totalItemCount-1;

if true load your data with regular fashion..

thaks Heiko Rupp for this snippet :)

This is what I used to load more data at the end of the listview

        listview.setOnScrollListener(new OnScrollListener(){

        @Override
        public void onScroll(AbsListView view,
        int firstVisibleItem, int visibleItemCount,
        int totalItemCount) {
        //Algorithm to check if the last item is visible or not
        final int lastItem = firstVisibleItem + visibleItemCount;
        if(lastItem == totalItemCount){                 
        // you have reached end of list, load more data             
        }
       } 
        @Override
        public void onScrollStateChanged(AbsListView view,int scrollState) {
        //blank, not using this
        }
        });

I had this issue when I was accidentally trying to update a view from a non-ui thread. Pushing the ui update code to the ui thread resolved my issue!

Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
    @Override
    public void run() {
            // insert ui update code here
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM