1.把”滑动查看更多…”的view控件填充到布局中
mLoadMoreView = getLayoutInflater().inflate(
R.layout.activity_check_comment_load_more, null)
mLoadMoreButton = (TextView) mLoadMoreView
.findViewById(R.id.load_more_button)
mListView.addFooterView(mLoadMoreView)
"滑动查看更多..."的view的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/load_more_button"
android:layout_width="fill_parent"
android:layout_height="@dimen/dp_30"
android:gravity="center"
android:textColor="@color/black"
android:text="@string/load_more_button"
android:background="@drawable/item_hotel_sort_condition_background_selector" />
</LinearLayout>
2.listview实现滚动监听事件
mListView.setOnScrollListener(new ScrollListener());
/**
* listview的滚动监听器
*
* @author Administrator
*
*/
private final class ScrollListener implements OnScrollListener {
private int mItemsLastIndex;
private int mLastIndex;
private int mVisibleLastIndex;
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mVisibleLastIndex = firstVisibleItem + visibleItemCount - 1;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
mItemsLastIndex = mAdapter.getCount() - 1;
mLastIndex = mItemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& mVisibleLastIndex == mLastIndex) {
mLoadMoreButton.setText("正在加载中...");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
requestMoreServerDatas(mRoomType, mCommentType,
mPageNumber);
}
}, 2000);
}
}
}
4.requestMoreServerDatas()调这个方法得到数据后
if ("0".equals(Status) && mList == null) {
mListView.removeFooterView(mLoadMoreView);
T.show(CheckCommentActivity.this, R.string.load_complete_datas, 0);
return;
}
for (int i = 0; i < mList.size(); i++) {
mCommentsDetailList.add(mList.get(i));
}
if (mList.size() < 10) {
T.show(CheckCommentActivity.this, R.string.load_complete_datas, 0);
mListView.removeFooterView(mLoadMoreView);
} else {
mLoadMoreButton.setText("查看更多...");
mPageNumber++;
}
5.如果是多种情况不停切换(比如:查看不同评论)![这里写图片描述](https://img-blog.csdn.net/20160715165110754)
这种情况要注意:切换前一定要先移除view,然后又添加view,避免一些特殊情况导致bug出现,并且页数重新赋值为2。
mPageNumber = 2
mListView.removeFooterView(mLoadMoreView)
mListView.addFooterView(mLoadMoreView)