现在基本使用RecyclerView来代替ListView了,但是他的item点击事件和上拉加载都需要自己来实现。下拉刷新可以使用SwipeRefreshLayout包裹RecyclerView来实现,很简单就不在说了。
直接贴代码了吧。详细东西都在注释里面了
注意:
1.如果你的item是多种布局的,记得在activity中调用adapter的setIsShowFootView(true);否则会报错,或者多布局不显示。
2.如果是单一布局的item,在adapter中getDisplayType方法空实现就行。setIsShowFootView(true)同样不应该调用。
3.默认在不加载更多时,footview是不在显示的,如果你想在没有更多时也显示一个footview需要调用setIsAlwaysShowFootView(true);
/**
* Created by LiJZ on 2016/5/16.
* 当isLoadMore为true时,即下拉加载更多生效时,肯定有footview
* 当设置了下拉加载更多的事件后,也可以设置sLoadMore为false,加载事件不生效。
* <p>
* 注意:当加载的itemview不适应屏幕时,设置:
* RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
* mFragmentView.setLayoutParams(params);
*/
public abstract class BaseRVAdapter06<T> extends RecyclerView.Adapter {
//每次的数据不同,需要重写定义
protected List<T> mListData;
protected Context mContext;
//item的布局view使用LayoutInflater加载
protected final LayoutInflater mLayoutInflater;
//item点击事件的接口
private OnRVItemClickListener mRVItemClickListener;
//item是否有点击事件,默认为false
private boolean isItemClick = false;
//是否使用自定义的footview,标志位
protected boolean isCustomFootView = false;
//是否加载更多,默认为false不加载
private boolean isLoadMore = false;
//显示footview的标志位
private boolean isShowFootView = false;
//footview的type
protected int FOOT_TYPE = 0430;
private OnLoadMoreListener mLoadMoreListener;
public BaseRVAdapter06(List<T> mListData, Context context) {
this.mContext = context;
this.mListData = mListData;
mLayoutInflater = LayoutInflater.from(context);
}
public BaseRVAdapter06(Context context) {
this.mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
/**
* 设置数据
*
* @param listData
*/
public void setListData(List<T> listData) {
this.mListData = listData;
notifyDataSetChanged();
}
/**
* 获取数据
*
* @return
*/
public List<T> getListData() {
return mListData;
}
/**
* 显示footview,默认跟随loadmore,当loadmore为true是显示footview否则不显示
* 当设为true时,都会显示一个footview
*/
public void setIsShowFootView(boolean isShowFootView) {
this.isShowFootView = isShowFootView;
}
/**
* 设置是否底部显示加载更多,默认不显示
*
* @param isLoadMore
*/
public void setIsLoadMore(boolean isLoadMore) {
this.isLoadMore = isLoadMore;
}
@Override
public int getItemCount() {
if ((isShowFootView || isLoadMore))
return mListData == null ? 0 : mListData.size() + 1;
return mListData == null ? 0 : mListData.size();
}
@Override
public int getItemViewType(int position) {
if ((isShowFootView || isLoadMore) && position == getItemCount() - 1) {
return FOOT_TYPE;
}
if (getDisplayType(position) > 0) {
return getDisplayType(position);
}
return super.getItemViewType(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if ((isShowFootView || isLoadMore) && viewType == FOOT_TYPE) {
if (onCreateFootHolder(parent, viewType) != null) {
isCustomFootView = true;
return onCreateFootHolder(parent, viewType);
} else {
//默认情况的footview,可以修改
View view = mLayoutInflater.inflate(R.layout.item_footview, null);
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
return new FootHolder(view);
}
}
return onCreateDisplayHolder(parent, viewType);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//当是最底部加载更多的view时
if ((isShowFootView || isLoadMore) && position == getItemCount() - 1) {
if (!isCustomFootView) {//使用默认的footview
FootHolder footHolder = (FootHolder) holder;
if (isLoadMore) {
footHolder.mllLoadMore.setVisibility(View.VISIBLE);
footHolder.mtvNoMore.setVisibility(View.GONE);
if (mLoadMoreListener != null) {
// TODO: 2016/11/29 正式应用时修改
// handler.sendEmptyMessage(0);
handler.sendEmptyMessageDelayed(0, 500);
}
} else {
footHolder.mllLoadMore.setVisibility(View.GONE);
footHolder.mtvNoMore.setVisibility(View.VISIBLE);
}
} else {//使用自定义footview
if (isLoadMore) {
onFootViewLoading(holder, position);
if (mLoadMoreListener != null) {
// TODO: 2016/11/29 正式应用时修改
// handler.sendEmptyMessage(0);
handler.sendEmptyMessageDelayed(0, 500);
}
} else {
onFootViewLoadFinish(holder, position);
}
}
return;
}
//给recyclerview的item设置点击事件
if (isItemClick) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRVItemClickListener != null) {
mRVItemClickListener.setItemClickListener(v, position);
}
}
});
}
onBindItemViewHolder(holder, position);
}
/**
* 给item的viewholder绑定数据
*
* @param holder
* @param position
*/
public abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);
/**
* 如果你的item布局是多种,需要复写这个方法
*
* @param postion
* @return
*/
public abstract int getDisplayType(int postion);
/**
* 创建item的viewholder
*
* @param parent
* @param viewType
* @return
*/
public abstract RecyclerView.ViewHolder onCreateDisplayHolder(ViewGroup parent, int viewType);
/**
* 设置自定义FootView,如果返回为空则使用默认布局,如需设置自定义FootView需重写此方法
*
* @param parent
* @param viewType
* @return
*/
public RecyclerView.ViewHolder onCreateFootHolder(ViewGroup parent, int viewType) {
return null;
}
/**
* 正在加载时footview的设置,默认无操作,需要时重写此方法
*
* @param holder
* @param position
*/
public void onFootViewLoading(RecyclerView.ViewHolder holder, int position) {
}
/**
* 加载更多完成时的footview的设置,默认无操作,需要时重写此方法
*
* @param holder
* @param position
*/
public void onFootViewLoadFinish(RecyclerView.ViewHolder holder, int position) {
}
/**
* footview的viewholder
*/
class FootHolder extends RecyclerView.ViewHolder {
private TextView mtvNoMore;
private LinearLayout mllLoadMore;
public FootHolder(View itemView) {
super(itemView);
mtvNoMore = (TextView) itemView.findViewById(R.id.tv_no_more);
mllLoadMore = (LinearLayout) itemView.findViewById(R.id.ll_load_more);
}
}
/**
* 定义一个接口,处理recyclerview的item的点击事件(整个item的点击事件,item的某一项的点击事件,
* 需要在ViewHolder或者onBindItemViewHolder中具体设置)
*/
public interface OnRVItemClickListener {
/**
* @param v 该条目的view对象
* @param position 该位置对应的position
*/
void setItemClickListener(View v, int position);
}
/**
* 加载更多的接口,滑动到底部自动加载数据
*/
public interface OnLoadMoreListener {
void loadMore();
}
/**
* 设置item的点击事件,不包含footview
*
* @param mRVItemClickListener
*/
public void setOnRVItemClickListener(OnRVItemClickListener mRVItemClickListener) {//对外暴露的接口
this.mRVItemClickListener = mRVItemClickListener;
isItemClick = true;
}
/**
* 设置加载更多的监听事件
*
* @param moreListerner
*/
public void setOnLoadMoreListerner(OnLoadMoreListener moreListerner) {//对外暴露的接口 设置加载更多的监听事件
isLoadMore = true;
this.mLoadMoreListener = moreListerner;
}
/**
* 获取加载更多的监听事件
*
* @return
*/
public OnLoadMoreListener getMoreListerner() {//对外暴露的接口 获取加载更多的监听事件
return mLoadMoreListener;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mLoadMoreListener != null) {
mLoadMoreListener.loadMore();
}
}
};
public void addData(int position, T t) {
if (mListData != null) {
mListData.add(t);
notifyItemInserted(position);
}
}
}