适配器,大家都知道,不多说,直接上代码:
package com.cn.happiness.mvplib.baseadpter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.cn.happiness.mvplib.R;
import com.cn.happiness.mvplib.baseadpter.listener.OnRecyclerItemClickListener;
import com.cn.happiness.mvplib.baseadpter.listener.RequestLoadMoreListener;
import com.cn.happiness.mvplib.baseadpter.view.FooterView;
import com.cn.happiness.mvplib.baseadpter.view.LoadType;
import com.cn.happiness.mvplib.util.NetworkUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Happiness on 2017/4/17.
*/
public abstract classBaseRecyclerAdapterextendsRecyclerView.Adapter {
protectedContextmContext;
protected intmLayoutResId;
protectedLayoutInflatermLayoutInflater;
protectedListmData;
private booleanmLoadingMoreEnable=false;
private booleanmNextLoadingEnable=false;
private booleanmHeadAndEmptyEnable;// headerView and emptyView
private booleanmFootAndEmptyEnable;// footerView and emptyView
public booleanmEmptyEnable,mErrorEnable;
private booleanmMultiItemTypeEnable;
privateLoadTypemLoadType;
private static final intVIEW_TYPE_HEADER=0x00001111;//header
public static final intVIEW_TYPE_CONTENT=0x00002222;//content
private static final intVIEW_TYPE_FOOTER=0x00003333;//footer
public static final intVIEW_TYPE_EMPTY=0x00004444;//empty
public static final intVIEW_TYPE_LOADING=0x00005555;//loading
private static final intVIEW_TYPE_NOT_NETWORK=0x00006666;//not network
private intmItemHeight;
private intmLastPosition= -1;
private intmViewType= -1;
privateRequestLoadMoreListenermRequestLoadMoreListener;
privateOnRecyclerItemClickListeneronRecyclerItemClickListener;
privateViewmFooterView,mEmptyView,mErrorView,mContentView,mLoadView;
privateLinearLayoutmHeaderView;
publicBaseRecyclerAdapter(Context context,List data, intlayoutResId) {
this.mContext= context;
this.mData= data ==null?newArrayList<>() : data;
if(layoutResId !=0) {
this.mLayoutResId= layoutResId;
}
this.mLayoutInflater= LayoutInflater.from(mContext);
this.mLoadType= LoadType.CUSTOM;
}
publicBaseRecyclerAdapter(Context context,List data) {
this(context,data,0);
}
/**
*@paramposition
*@return
*/
publicTgetItem(intposition) {
returnmData.get(position);
}
@Override
public intgetItemCount() {
// int loadMoreCount = isLoadMore() ? 1 : 0;
// int count = mData.size() + getHeaderViewCount() + getFooterViewCount() + loadMoreCount;
// if (mData.size() == 0 && mEmptyView != null) {
// if (count == 0 && (!mHeadAndEmptyEnable || !mFootAndEmptyEnable)) {
// count += getEmptyViewCount();
// } else if (mHeadAndEmptyEnable || mFootAndEmptyEnable) {
// count += getEmptyViewCount();
// }
// if ((mHeadAndEmptyEnable && getHeaderViewCount() == 1 && count == 1) || count == 0) {
// mEmptyEnable = true;
// count += getEmptyViewCount();
// }
// }
// return count;
if(mViewType==VIEW_TYPE_CONTENT) {
returnmData.size();
}else{
return1;
}
}
public voidchangeItemViewType(inttype) {
this.mViewType= type;
this.notifyDataSetChanged();
}
@Override
public intgetItemViewType(intposition) {
if(!NetworkUtils.isNetworkConnected(mContext)) {
returnVIEW_TYPE_NOT_NETWORK;
}
if(mViewType==VIEW_TYPE_LOADING) {
returnVIEW_TYPE_LOADING;
}else if(mViewType==VIEW_TYPE_EMPTY) {
returnVIEW_TYPE_EMPTY;
}
returnVIEW_TYPE_CONTENT;
// if (mHeaderView != null && position == 0) {
// return VIEW_TYPE_HEADER;
// }
// // emptyView position <=2 (header + empty + footer)
// // four situation {@link #setEmptyView(header+empty+footer , empty , empty+footer,header+empty)} position <= 2
// if (mData.size() == 0 && mEmptyView != null && mEmptyEnable && position <= 2) {
//
// // three situation {@link #setEmptyView(header + empty + footer , header + empty , empty+footer)} position = 1
// if ((mHeadAndEmptyEnable || mFootAndEmptyEnable) && position == 1) {
//
// if (mHeaderView == null && mFooterView != null) { //empty+footer
// return VIEW_TYPE_FOOTER;
// } else if (mHeaderView != null && mEmptyView != null) { //header + empty + footer , header + empty
// return VIEW_TYPE_EMPTY;
// }
// }
// //two situation position = 0
// else if (position == 0) {
// if (mHeaderView == null) {
// return VIEW_TYPE_EMPTY;
// } else {
// return VIEW_TYPE_HEADER;
// }
// } else if (position == 2 && mHeaderView != null && mFooterView != null) {
// return VIEW_TYPE_FOOTER;
// } else if (position == 1) {
// if (mHeaderView != null) {
// return VIEW_TYPE_EMPTY;
// }
// return VIEW_TYPE_FOOTER;
// }
// }
// //position == mData.size() + getHeaderViewsCount()
// else if (position == mData.size() + getHeaderViewCount()) {
// if (mNextLoadingEnable) {
// return VIEW_TYPE_LOADING;
// }
// return VIEW_TYPE_FOOTER;
// }
// //type content
// else if (position - getHeaderViewCount() >= 0) {
// if (mMultiItemTypeEnable)
// return getMultiItemViewType(position - getHeaderViewCount());
// return VIEW_TYPE_CONTENT;
// }
// return super.getItemViewType(position - getHeaderViewCount());
}
protected intgetMultiItemViewType(intposition) {
return super.getItemViewType(position);
}
@Override
public voidonBindViewHolder(finalRecyclerView.ViewHolder holder, intposition) {
mViewType= holder.getItemViewType();
switch(mViewType) {
caseVIEW_TYPE_CONTENT:
if(mData.isEmpty()) {
return;
}
convert((BaseViewHolder) holder,mData.get(holder.getLayoutPosition() - getHeaderViewCount()));
break;
}
}
// @Override
// public void onAttachedToRecyclerView(RecyclerView recyclerView) {
// super.onAttachedToRecyclerView(recyclerView);
// RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
// if (manager instanceof GridLayoutManager) {
// final GridLayoutManager gridManager = ((GridLayoutManager) manager);
// gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
// @Override
// public int getSpanSize(int position) {
// int type = getItemViewType(position);
// return (type == VIEW_TYPE_EMPTY || type == VIEW_TYPE_HEADER || type == VIEW_TYPE_FOOTER
// || type == VIEW_TYPE_LOADING) ? gridManager.getSpanCount() : 1;
// }
// });
// }
// }
@Override
publicRecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {
BaseViewHolder baseViewHolder =null;
switch(viewType) {
caseVIEW_TYPE_HEADER:
baseViewHolder =newBaseViewHolder(mHeaderView);
break;
caseVIEW_TYPE_CONTENT:
baseViewHolder = onBaseViewHolder(parent);
initItemClickListener(baseViewHolder);
break;
caseVIEW_TYPE_FOOTER:
baseViewHolder =newBaseViewHolder(mFooterView);
break;
caseVIEW_TYPE_EMPTY:
mEmptyView=mLayoutInflater.inflate(R.layout.view_empty_material, null);
mEmptyView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
baseViewHolder =newBaseViewHolder(mEmptyView);
break;
caseVIEW_TYPE_LOADING:
mLoadView=mLayoutInflater.inflate(R.layout.view_loading_material, null);
mLoadView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
baseViewHolder =newBaseViewHolder(mLoadView);
//baseViewHolder = addLoadingView(mLoadType);
break;
caseVIEW_TYPE_NOT_NETWORK:
mErrorView=mLayoutInflater.inflate(R.layout.view_net_error_material, null);
mErrorView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
baseViewHolder =newBaseViewHolder(mErrorView);
break;
}
returnbaseViewHolder;
}
/**
*@paramhelper
*@paramitem
*/
protected abstract voidconvert(BaseViewHolder helper,Titem);
/**
*@paramloadType
*@return
*/
publicBaseViewHolderaddLoadingView(LoadType loadType) {
// mLoadView = new FooterView(mContext);
if(mLoadViewinstanceofFooterView) {
((FooterView)mLoadView).setLoadView(loadType);
}
return newBaseViewHolder(mLoadView);
}
/**
*@paramparent
*/
protectedBaseViewHolderonBaseViewHolder(ViewGroup parent) {
returncreateBaseViewHolder(parent,mLayoutResId);
}
/**
*@paramparent
*@paramlayoutResId
*@return
*/
protectedBaseViewHoldercreateBaseViewHolder(ViewGroup parent, intlayoutResId) {
if(mContentView==null) {
return newBaseViewHolder(getItemView(layoutResId,parent));
}
return newBaseViewHolder(mContentView);
}
/**
*@paramlayoutResId
*@paramparent
*@return
*/
protectedViewgetItemView(intlayoutResId,ViewGroup parent) {
View view =mLayoutInflater.inflate(layoutResId,parent, false);
if(mItemHeight!=0) {
view.getLayoutParams().height=mItemHeight;
}
returnview;
}
/**
*@parambaseViewHolder
*/
private voidinitItemClickListener(finalBaseViewHolder baseViewHolder) {
if(onRecyclerItemClickListener!=null) {
baseViewHolder.itemView.setOnClickListener(v ->onRecyclerItemClickListener.onItemClick(v,baseViewHolder.getLayoutPosition() - getHeaderViewCount()));
}
}
/**
* add loadMore interface
*/
public voidaddLoadMore() {
if(isLoadMore() && !mLoadingMoreEnable) {
mLoadingMoreEnable=true;
mRequestLoadMoreListener.onLoadMoreRequested();
}
}
/**
* is loaded more
*/
private booleanisLoadMore() {
returnmNextLoadingEnable&&mRequestLoadMoreListener!=null&&mData.size() >0;
}
/**
*@return
*/
public intgetHeaderViewCount() {
returnmHeaderView==null?0:1;
}
/**
*@return
*/
public intgetFooterViewCount() {
returnmFooterView==null?0:1;
}
/**
*@return
*/
public intgetEmptyViewCount() {
returnmEmptyView==null?0:1;
}
/**
*@paramposition
*@paramitem
*/
public voidadd(intposition,Titem) {
mData.add(position,item);
notifyItemInserted(position);
}
/**
*@paramdatas
*/
public voidaddAll(List datas) {
if(datas !=null) {
mData.addAll(datas);
notifyDataSetChanged();
}
}
/**
*@paramitem
*/
public voidadd(Titem) {
add(mData.size(),item);
}
/**
*@paramemptyView
*/
public voidaddEmptyView(View emptyView) {
addEmptyView(false, false,emptyView);
}
/**
*@paramisHeadAndEmpty
*@paramemptyView
*/
public voidaddEmpty(booleanisHeadAndEmpty,View emptyView) {
addEmptyView(isHeadAndEmpty, false,emptyView);
}
/**
*@paramfooter
*/
public voidaddFooterView(View footer) {
mNextLoadingEnable=false;
this.mFooterView= footer;
this.notifyDataSetChanged();
}
/**
*@paramheader
*/
public voidaddHeaderView(View header) {
if(mHeaderView==null) {
mHeaderView=newLinearLayout(mContext);
mHeaderView.setOrientation(LinearLayout.VERTICAL);
mHeaderView.setLayoutParams(newViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
}
if(header !=null) {
try{
this.mHeaderView.addView(header);
}catch(RuntimeException e) {
this.mHeaderView.removeAllViews();
this.mHeaderView.addView(header);
}
this.notifyDataSetChanged();
}
}
/**
* add no more data view
*/
public voidaddNoMoreView() {
mNextLoadingEnable=false;
mFooterView=newFooterView(mContext);
((FooterView)mFooterView).setNoMoreView();
this.notifyDataSetChanged();
}
/**
* add a loadingView
*
*@paramloadingView
*/
public voidaddLoadingView(View loadingView) {
this.mLoadView= loadingView;
}
/**
*@paramisHeadAndEmpty
*@paramisFootAndEmpty
*@paramemptyView
*/
public voidaddEmptyView(booleanisHeadAndEmpty, booleanisFootAndEmpty,View emptyView) {
mHeadAndEmptyEnable= isHeadAndEmpty;
mFootAndEmptyEnable= isFootAndEmpty;
mEmptyView= emptyView;
mEmptyEnable=true;
}
/**
*@paramenable
*/
public voidopenLoadingMore(booleanenable) {
this.mNextLoadingEnable= enable;
}
/**
*@parammultiItemTypeEnable
*/
public voidopenMultiItemType(booleanmultiItemTypeEnable) {
this.mMultiItemTypeEnable= multiItemTypeEnable;
}
/**
*@paramdata
*/
public voidsetData(List data) {
this.mData= data;
// if (mRequestLoadMoreListener != null) {
// mNextLoadingEnable = true;
// mFooterView = null;
// }
// mLastPosition = -1;
notifyDataSetChanged();
}
/**
*@paramitemHeight
*/
public voidsetItemHeight(intitemHeight) {
this.mItemHeight= itemHeight;
}
/**
*@paramdata
*/
public voidaddData(List data) {
this.mData.addAll(data);
notifyDataSetChanged();
}
/**
*@return
*/
publicListgetData() {
returnmData;
}
/**
*@paramonRecyclerViewItemClickListener
*/
public voidsetOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerViewItemClickListener) {
this.onRecyclerItemClickListener= onRecyclerViewItemClickListener;
}
/**
*@paramrequestLoadMoreListener
*/
public voidsetOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListener) {
this.mRequestLoadMoreListener= requestLoadMoreListener;
}
}