dynamic listview adding Load more items at the end of scroll
you try the following code
list.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
additems();
}
}
}
});
in the additem()
add the next 10 items to array list and set the flag_loading
as false
. and call notifydatasetchanged
. it should work.
How can I load more items when scrolling down in listview?
The approach I use in my apps is:
Step # 1: I load first set of data with limit from server and then store the last record data id in a variable.
Step # 2: Implement the on scroll end listener to your listView or RecyclerView.
Step # 3: Start another request inside on scroll end listener and load new records.(Here I set again data id in a variable)
For Example:
Start the request when the activity starts and do what I explained earlier.
GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId());
Then inside your on Scroll End Listener
GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId()+"&limit=10&lastDataId="+BusinessItems[index].mBusinessId)
Edit To avoid duplicate api call inside GetBusinesses() check if the request was started previously, well the idea is create a boolean initially false and then in your GetBusinesses() function make it true before starting the request and once the data is loaded and request is finish make it false again.
I hope this help.
Modern way of doing automatic load more in Android ListView/RecyclerView
The Android Jetpack Paging library is the current recommendation here.
You should use a RecyclerView
instead of a ListView
, and then you can create an adapter that subclasses PagedListAdapter
. From there, you can created PagedList
instances from your data source, and submit them to your adapter via submitList
. You can find more information and a detailed walkthrough in the documentation for the Paging library.
Android - ListView to load more items when reached end
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
Related Topics
Get List of Photo Galleries on Android
Android: Wrap_Content Is Not Working with Listview
Intent.Resolveactivity Returns Null in API 30
Download Files and Store Them Locally with Phonegap/Jquery Mobile Android and iOS Apps
Check If Correct Google Play Service Available: "Unfortunately Application Has Stopped Working"
Using Jaxb with Google Android
Can't Run a Java Android Program with Valgrind
Problem to Load Flv Video in Webview
Android Setonclicklistener Method - How Does It Work
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
Current Month Facebook Friends Birthdays in Android
Android Onclick in Xml VS. Onclicklistener
Blur Background Behind Alertdialog
How to Style the Drawerarrowtoggle from Android Appcompat V7 21 Library