Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView to load more items when reached end [closed]

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.

like image 889
buddy123 Avatar asked Dec 08 '13 20:12

buddy123


1 Answers

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

Hope it will help

like image 91
Md. Monsur Hossain Tonmoy Avatar answered Oct 24 '22 21:10

Md. Monsur Hossain Tonmoy