listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// SCROLL_STATE_IDLE, SCROLL_STATE_TOUCH_SCROLL or
// SCROLL_STATE_IDLE.
switch (scrollState) {
case SCROLL_STATE_IDLE:
if (isBottom == true) {
if (pageNum < 4) {
pageNum++;
MyTask task = new MyTask(listview, MainActivity.this);
task.execute(URL + pageNum);
} else {
Toast.makeText(MainActivity.this, "已经是最后一页了", Toast.LENGTH_LONG).show();
}
}
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
isBottom = true;
} else {
isBottom = false;
}
}
});
备注:
AbsListView.OnScrollListener
OnScrollListener.onScrollStateChanged()可监听如下事件:
AbsListView.OnScrollListener.SCROLL_STATE_IDLE://滑动停止,即松手
AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滑动
AbsListView.OnScrollListener.SCROLL_STATE_FLING://手指用力滑动(手指离开时,listview会由于惯性继续滑动)
OnScrollListener.onScroll()方法则当滚动时一直回调,有如下参数
firstVisibleItem://当前能看到的第一个Item的ID(从0开始)
visibleItemCount://当前能看到的Item的总数
totalItemCount://整个ListView的Item总数