I'm having problem with my setOnScrollListener. It just keeps calling my asynctask whenever I scroll to the bottom of the listview. How do I set the setOnScrollListener to load only once I reach the bottom.
listview.setAdapter(adapter);
mProgressDialog.dismiss();
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if (lastInScreen == totalItemCount) {
new loadmore().execute();
} else {
}
}
);
These are the top rated real world Java examples of android.content.ListView.setOnItemClickListener extracted from open source projects. You can rate examples to help us improve the quality of examples.
An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView. Callback method to be invoked when RecyclerView's scroll state changes. Callback method to be invoked when the RecyclerView has been scrolled. Callback method to be invoked when RecyclerView's scroll state changes.
This will be called after the scroll has completed. This callback will also be called if visible item range changes after a layout calculation. In that case, dx and dy will be 0. RecyclerView: The RecyclerView which scrolled. int: The amount of horizontal scroll. int: The amount of vertical scroll.
OnScroll method is called whenever you scroll down the list view, so the best bet for you to is to use some kind of padding, like the one implemented here.
public class EndlessScrollListener implements OnScrollListener
private int visibleThreshold = 5;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// I load the next page of gigs using a background task,
// but you can call any function here.
new LoadGigsTask().execute(currentPage + 1);
loading = true;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
visibleThreshold – The minimum amount of items to have below your current scroll position, before loading more.
currentPage – The current page of data you have loaded
previousTotal – The total number of items in the dataset after the last load
loading – True if we are still waiting for the last set of data to load.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With