public final class ScrollListener implements AbsListView.OnScrollListener {
private boolean loadFinish;
private LoadCallBack callBack;
public void setLoadFinish(boolean loadFinish) {
this.loadFinish = loadFinish;
}
public ScrollListener(LoadCallBack callBack) {
this.callBack = callBack;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastItemid = view.getLastVisiblePosition(); // 获取当前屏幕最后Item的ID
if ((lastItemid + 1) == totalItemCount) { // 达到数据的最后一条记录
if (totalItemCount > 0) {
if (loadFinish) {
loadFinish = false;
if (callBack != null) {
callBack.load();
}
}
}
}
}
public interface LoadCallBack {
void load();
}
}
使用方式
//到底加载
ScrollListener scrollListener = new ScrollListener(new ScrollListener.LoadCallBack() {
@Override
public void load() {
//加载新数据
}
});
listview.setOnScrollListener(scrollListener);
请求数据成功后必须调用下列代码!!!!
scrollListener.setLoadFinish(true);