第一部分转载
参考:http://blog.csdn.net/jodan179/article/details/8017693
http://blog.csdn.net/fxtxz2/article/details/8450002
ListView.OnScrollListener的 onScrollStateChanged(AbsListView view, int scrollState) 方法,使用说明如下:
ListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
Log.v("已经停止:SCROLL_STATE_IDLE");
break;
case OnScrollListener.SCROLL_STATE_FLING:
Log.v("开始滚动:SCROLL_STATE_FLING");
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
Log.v("正在滚动:SCROLL_STATE_TOUCH_SCROLL");
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
监听ListView滚动到最底部使用 onScrollStateChanged(AbsListView view, int scrollState) 方法,代码大致如下:
// 监听listview滚到最底部
mIndexList.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
// 当不滚动时
case OnScrollListener.SCROLL_STATE_IDLE:
// 判断滚动到底部
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
isLastisNext++;
}
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
1、监听ListView滚动到最底部
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { isScrolling = true; return; } isScrolling = false; // 判断是否滚动到底部 if ((view.getLastVisiblePosition() == view.getCount() - 1) && !isAllListItemShowInOnePage) { //加载更多功能的代码 loadDatas(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { isAllListItemShowInOnePage = (visibleItemCount == totalItemCount); }
2、滚动到最底部时加载分页数据loadDatas();
@Override public void loadDatas() { fetchGameListByPage(mEndPage + 1); }其中
mEndPage初始值为0,在请求数据成功onSuccess的时候mEndPage++
isAllListItemShowInOnePage判断数据量很少,已在一屏显示完的情况
@Override public void onSuccess(ExtendPageInfo extendPageInfo) { if (!isActivityValid()) { return; } trackPageLoad(); updateDatas(extendPageInfo); updateUI(); }
protected void updateDatas(IResponseable data) { mIsAutoLoad = false; isLoaddingDatas = false; mPageCount = getPageCount(data); mEndPage++; }
@Override public void onSuccess(ListInfo ListInfo) { if (!isActivityValid()) { return; } trackPageLoad(); updateDatas(presentListInfo); updateUI(); }
private void updateDatas(ListInfo ListInfo) { super.updateDatas(ListInfo); ListInfo> Infos = ListInfo.getAllPresents(); mList.addAll(Infos); // adapter数据初始化 mAdapter.setData(mList); mAdapter.notifyDataSetChanged(); }