直接上代码

作者:内容

本文是来自于RecyclerView添加Header的正确方式(http://blog.csdn.net/qibin0506/article/details/49716795)的一个扩展。实现原理作者已经介绍的很清楚,就不再赘述。原文作者针对于Header的添加做了详细介绍,本人对其稍加修改,添加了Footer的部分。

图片 1WechatIMG1.jpeg

可以方便解决类似上图对齐的困扰,话不多少,直接上代码

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; } 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; 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 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 == TYPE_HEADER || getItemViewType == TYPE_FOOTER) return; final int pos = getRealPosition(viewHolder); final T data = mDatas.get; onBind(viewHolder, pos, data); if (mListener != null) { viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick { 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 == TYPE_HEADER || getItemViewType == TYPE_FOOTER) ? gridManager.getSpanCount() : 1; } }); } } @Override public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) { super.onViewAttachedToWindow; ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) { StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; p.setFullSpan(holder.getLayoutPosition; } } 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 : (mFooterView == null ? mDatas.size() + 1 : mDatas.size; } 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; } } public interface OnItemClickListener<T> { void onItemClick(int position, T data); }}

本文由澳门十三第发布,转载请注明来源

关键词: