[英]Android ListView setOnScrollListener
I'm having problem with my setOnScrollListener.我的 setOnScrollListener 有问题。 It just keeps calling my asynctask whenever I scroll to the bottom of the listview.
每当我滚动到列表视图的底部时,它就会一直调用我的 asynctask。 How do I set the setOnScrollListener to load only once I reach the bottom.
如何将 setOnScrollListener 设置为仅在到达底部时加载。
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 {
}
}
);
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 implementedhere .每当您向下滚动列表视图时都会调用 OnScroll 方法,因此最好的办法是使用某种填充,例如此处实现的填充。
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 currentPage – 您加载的当前数据页面
previousTotal – The total number of items in the dataset after the last load previousTotal – 上次加载后数据集中的项目总数
loading – True if we are still waiting for the last set of data to load. loading – 如果我们仍在等待最后一组数据加载,则为真。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.