Android Dynamically load Listview at scroll end?

后端 未结 6 1887
梦毁少年i
梦毁少年i 2020-12-04 07:37

I am loading a listview from Http server, 20 at a time ,At the end of Listview I a want to load next 20 data from server and this process will continue till

相关标签:
6条回答
  • 2020-12-04 08:03

    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
            }
            });
    
    0 讨论(0)
  • 2020-12-04 08:03

    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
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-04 08:05

    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.

    0 讨论(0)
  • 2020-12-04 08:05

    Ok i have seen that this is a common problem, so i have made a SMALL LIBRARY with custom load more and pull to refresh ListView in github, please check this at here, all is very explained in the repository.

    0 讨论(0)
  • 2020-12-04 08:14

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

    See e.g.

    ....
    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.

    0 讨论(0)
  • 2020-12-04 08:21

    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 :)

    0 讨论(0)
提交回复
热议问题