本文是來自于
RecyclerView添加Header的正確方式(http://blog.csdn.net/qibin0506/article/details/49716795)
的一個(gè)擴(kuò)展井辜。實(shí)現(xiàn)原理作者已經(jīng)介紹的很清楚,就不再贅述肤无。原文作者針對(duì)于Header的添加做了詳細(xì)介紹绰咽,本人對(duì)其稍加修改,添加了Footer的部分楞捂。
可以方便解決類似上圖對(duì)齊的困擾梁丘,話不多少,直接上代碼
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_HEADER = 0;
public static final int TYPE_NORMAL = 1;
public static final int TYPE_FOOTER = 2;
private ArrayList<T> mDatas = new ArrayList<>();
private View mHeaderView,mFooterView;
private OnItemClickListener mListener;
public void setOnItemClickListener(OnItemClickListener li) {
mListener = li;
}
public void setHeaderView(View headerView) {
mHeaderView = headerView;
notifyItemInserted(0);
}
public View getHeaderView() {
return mHeaderView;
}
public View getFooterView() {
return mFooterView;
}
public void setFooterView(View mFooterView) {
this.mFooterView = mFooterView;
}
public void addDatas(ArrayList<T> datas) {
mDatas.addAll(datas);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if (mHeaderView == null && mFooterView == null) return TYPE_NORMAL;
if (position == 0 && mHeaderView != null) return TYPE_HEADER;
if (mFooterView != null){
if (mHeaderView == null && position == mDatas.size()) return TYPE_FOOTER;
if (mHeaderView != null && position == mDatas.size() + 1) return TYPE_FOOTER;
}
return TYPE_NORMAL;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
if (mHeaderView != null && viewType == TYPE_HEADER) return new Holder(mHeaderView);
if (mFooterView != null && viewType == TYPE_FOOTER) return new Holder(mFooterView);
return onCreate(parent, viewType);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (getItemViewType(position) == TYPE_HEADER || getItemViewType(position) == TYPE_FOOTER) return;
final int pos = getRealPosition(viewHolder);
final T data = mDatas.get(pos);
onBind(viewHolder, pos, data);
if (mListener != null) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(pos, data);
}
});
}
}
@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) {
return (getItemViewType(position) == TYPE_HEADER || getItemViewType(position) == TYPE_FOOTER)
? gridManager.getSpanCount() : 1;
}
});
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if (lp != null
&& lp instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(holder.getLayoutPosition() == 0);
}
}
public int getRealPosition(RecyclerView.ViewHolder holder) {
int position = holder.getLayoutPosition();
return mHeaderView == null ? position : position - 1;
}
@Override
public int getItemCount() {
return mHeaderView == null ? (mFooterView == null ? mDatas.size() : mDatas.size() + 1) : (mFooterView == null ? mDatas.size() + 1 : mDatas.size() + 2);
}
public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType);
public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data);
public class Holder extends RecyclerView.ViewHolder {
public Holder(View itemView) {
super(itemView);
}
}
public interface OnItemClickListener<T> {
void onItemClick(int position, T data);
}
}