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.
Asked
Active
Viewed 3.2k times
1 Answers
31
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

Md. Monsur Hossain Tonmoy
- 11,045
- 2
- 22
- 19
-
thank you, I'll try this and see how it turns out :) – buddy123 Dec 08 '13 at 20:55
-
Why not use java.util.concurrent.Semaphore to make things even more solid ? – slott Mar 26 '15 at 14:42
-
@Md. Monsur Hossain Tonr where is the class for onScrollListener – Erum Sep 12 '15 at 12:47
-
where we have to make `isLoading` false? – pRaNaY Nov 10 '15 at 08:40
-
1@pRaNaY You are supposed to reset `isLoading` after `loadMoreData()` completed. – Jeremy May 25 '16 at 13:12