标签:
模板代码
package xiaoxu.com.lifeservice.ui.loadmore;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import xiaoxu.com.lifeservice.R;
import xiaoxu.com.lifeservice.http.HttpClient;
/**
* Created by Administrator on 2016/5/4.
* 用法:和普通的listView一样,在xml文件中指定,不过这里应该指定为 LoadMoreListView
* 提供了setmOnLoadMoreListener()公有的方法,接口回调机制,完成加载动作。
* 注意:这里的 HttpClient.PAGE_SIZE 是指该listView应该默认为加载多少条item的数量.
*
*/
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
Boolean mIsLoadingMore = false;
Boolean mLastItemVisible;
OnScrollListener mOnScrollListener;
public OnLoadMoreListener mOnLoadMoreListener;
RelativeLayout mFooterView;
ProgressBar mProgressBar;
TextView mTextView;
public void setmOnScrollListener(OnScrollListener mOnScrollListener) {
this.mOnScrollListener = mOnScrollListener;
}
public LoadMoreListView(Context context) {
super(context);
init();
}
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
mFooterView = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_load_more, null, false);
mProgressBar = (ProgressBar) mFooterView.findViewById(R.id.progress_bar);
mTextView = (TextView) mFooterView.findViewById(R.id.text);
removeFooterView(mFooterView);
setFooterDividersEnabled(false);
addFooterView(mFooterView);
super.setOnScrollListener(this);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (!mIsLoadingMore && scrollState == OnScrollListener.SCROLL_STATE_IDLE && mLastItemVisible) {
mIsLoadingMore = true;
onLoadMore(); //加载一次数据 接口回调
}
if( mOnScrollListener != null ) {
mOnScrollListener.onScrollStateChanged(view,scrollState);
}
}
public void setmOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener;
}
private void onLoadMore() {
if(mIsLoadingMore != null) {
mOnLoadMoreListener.onLoadMore();
}
}
/**
* Set whether the Last Item is Visible. lastVisibleItemIndex is a
* zero-based index, so we minus one totalItemCount to check
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (null != mOnLoadMoreListener) {
mLastItemVisible = (totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1);
}
// Finally call OnScrollListener if we have one
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
public void updateLoadMoreViewText (List data) {
if(getAdapter().getCount() == 0 || data.isEmpty()) {
setLoadMoreViewTextNoData();
} else if (data.size() < HttpClient.PAGE_SIZE) {
//每一页要加载的item的数量,当最后一次加载的数据小于这个值后,设置以加载全部。
setLoadMoreViewTextNoMoreData();
}
mIsLoadingMore = false;
}
public void onLoadMoreComplete(){
mIsLoadingMore = false;
}
public void setLoadMoreViewTextError() {
mTextView.setText("网络异常,加载重试");
mProgressBar.setVisibility(GONE);
mIsLoadingMore = false;
}
public void setLoadMoreViewTextNoData() {
mTextView.setText("暂无数据");
mProgressBar.setVisibility(GONE);
mIsLoadingMore = false;
}
public void setLoadMoreViewTextNoMoreData() {
mTextView.setText("已加载全部");
mProgressBar.setVisibility(GONE);
mIsLoadingMore = false;
}
public void setLoadMoreViewText(String text) {
mTextView.setText(text);
mProgressBar.setVisibility(VISIBLE);
}
public void setLoadMoreViewText(int resId) {
mTextView.setText(resId);
mProgressBar.setVisibility(VISIBLE);
}
public interface OnLoadMoreListener {
void onLoadMore();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="false"
android:gravity="center">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:indeterminate="false"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/progress_bar"
android:text="正在载入...."
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</RelativeLayout>
使用Demo
private void loadData() {
if (isLoadAll) {
return;
}
searchParam.setPno(pno);//SearchParam封装的请求信息参数的类 如经纬度、城市...
HttpClient.getRecommendShops(searchParam, new HttpResponseHandle() {
//静态方法回调
@Override
public void onSuccess(String content) {
mPtrFrame.refreshComplete();
JSONObject object = JSON.parseObject(content);
List<SearchShop> list = JSONArray.parseArray(object.getString("body"), SearchShop.class);
for(SearchShop s : list) {
Log.v("TextFrag", " name " + s.getName() + "\n");
}
Log.v("TextFrag","------");
listView.updateLoadMoreViewText(list);
isLoadAll = list.size() < HttpClient.PAGE_SIZE;
Log.v("TextFrag"," listSize " + list.size());
if (pno == 1) {
quickAdapter.clear();
}
quickAdapter.addAll(list);
pno++;
}
@Override
public void onFailure(Request request, IOException e) {
mPtrFrame.refreshComplete();
}
});
}
listView.setmOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
Log.v("TextFrag","加载更多");
loadData();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//UiHelper.showHouseDetailActivity(mActivity);
}
});
标签:
原文地址:http://blog.csdn.net/qq_32223565/article/details/51356978