import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import java.util.List;
/**
* Created by Administrator on 2016/2/26 0026.
*/
public abstract class BaseListViewAdapter<T> extends BaseAdapter {
private List<T> list;
protected Context context;
private LoadMoreListener loadMoreListener;//加载更多接口
public boolean isLoading = false;//是否正在加载
private int currentPage = 0;//当前页
private boolean isFirstLoad = true;//是否第一次调用网络数据
public void setRefreshAndLoadMoreListener(ListView listView, LoadMoreListener listener) {
this.loadMoreListener = listener;
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//正在加载数据时禁止再次加载
if (isLoading) {
return;
}
//当本地数据和网络数据一致时不再加载
if (!isFirstLoad&&getCount() >= loadMoreListener.getAllQueryItemCount()) {
return;
}
if (firstVisibleItem + visibleItemCount + loadMoreListener.getReverseItemIndexLoadMore() > totalItemCount + 1) {
isLoading = true;
loadMoreListener.loadMore(currentPage + 1);
}
}
});
}
/**
* 第一次加载或者刷新时需要调用
*/
public void startRefresh() {
isLoading = true;
}
/**
* 第一次加载或者刷新后需要调用
* @param success
*/
public void refreshResult(boolean success) {
if (success) {
currentPage = 0;
isFirstLoad = false;
}
isLoading = false;
notifyDataSetChanged();
}
/**
* 加载更多后需要调用
* @param success
*/
public void loadMoreResult(boolean success) {
if (success) {
currentPage++;
isFirstLoad = false;
}
isLoading = false;
notifyDataSetChanged();
}
public BaseListViewAdapter(List<T> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = getItemViewResource();
recordAllWidget(holder.sparseArray, convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
bindItemData(holder.sparseArray, list.get(position), position);
return convertView;
}
//重新此方法返回root view
public abstract View getItemViewResource();
//在这个方法中进行view和数据的绑定
public abstract void bindItemData(SparseArray<View> sparseArray, T t, int position);
//sparseArray中需要根据id记录下所有需要用到的子view
public abstract void recordAllWidget(SparseArray<View> sparseArray, View main);
class ViewHolder {
public SparseArray<View> sparseArray = new SparseArray();
}
interface LoadMoreListener {
/**
* 返回网络上查询的所有记录数
*
* @return
*/
int getAllQueryItemCount();
/**
* 距离剩下几个item开始自动加载更多
*
* @return
*/
int getReverseItemIndexLoadMore();
/**
* 加载下一页
*
* @param currentPage
*/
void loadMore(int currentPage);
}
}
网友评论