实现该功能需要用到listview的setOnScrollListener这个监听器。
private boolean scrollFlag = false;// 标记是否滑动
private int lastVisibleItemPosition;// 标记上次滑动位置
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
scrollFlag = true;
} else {
scrollFlag = false;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("dc", "firstVisibleItem::" + firstVisibleItem +
":visibleItemCount:" + visibleItemCount + ":totalItemCount:" +
totalItemCount);
if (scrollFlag) {
if (firstVisibleItem > lastVisibleItemPosition) {
Log.d("dc", "上滑");
}
if (firstVisibleItem < lastVisibleItemPosition) {
Log.d("dc", "下滑");
}
if (firstVisibleItem == lastVisibleItemPosition) {
return;
}
lastVisibleItemPosition = firstVisibleItem;
}
}