一、把ListView尾部布局加入到ListView中
moreView = mInflater.inflate(R.layout.load, null);
mListView.addFooterView(moreView);
二、设置ListView的OnScrollListener监听
mListView.setSelected(true);
mListView.setOnScrollListener(mOnScrollListener);
private AbsListView.OnScrollListener mOnScrollListener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (isLastRow && scrollState == this.SCROLL_STATE_IDLE) {
LogUtils.i(TAG, "拉到最底部");
mMoreProgressBar.setVisibility(View.VISIBLE);
mMoreText.setText(AppliteUtils.getString(mContext, R.string.loading));
moreView.setVisibility(View.VISIBLE);
if (isToEnd) {
mMoreProgressBar.setVisibility(View.GONE);
mMoreText.setText(AppliteUtils.getString(mContext, R.string.no_data));
} else {
//加载更多数据,这里可以使用异步加载
if (isSearchPost) {
postSearch(mEtView.getText().toString());//网络请求数据
isSearchPost = false;
}
}
LogUtils.i(TAG, "加载更多数据");
isLastRow = false;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
//判断是否滚到最后一行
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
LogUtils.i(TAG, "滚到最后一行");
isLastRow = true;
}
}
};
在得到数据后设置moreView尾部布局隐藏
三、XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:orientation="horizontal"
android:gravity="center"
android:visibility="gone">
<ProgressBar
android:id="@+id/load_progressbar"
style="?android:attr/progressBarStyleSmall"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="@color/black"
android:layout_marginLeft="20dp"
android:id="@+id/loadmore_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dip"
android:text="@string/loading" />
</LinearLayout>