RecyclerView 通用适配器
原文地址:https://www.jianshu.com/p/82a74c9ccba5
public abstract class RVBaseAdapter<T> extends RecyclerView.Adapter<RVBaseAdapter.CommonViewHolder> { protected List<T> mBeans; protected Context mContext; public static final int TYPE_NORMAL = 0; //说明是不带有header和footer的 public static final int TYPE_HEADER = -1; //说明是带有Header的 public static final int TYPE_FOOTER = -2; //说明是带有Footer的 //HeaderView, FooterView private boolean mHaveHeader; private boolean mHaveFooter; public RVBaseAdapter(Context context, List<T> beans, boolean haveHead, boolean haveFoot) { mContext = context; mBeans = beans; mHaveHeader = haveHead; mHaveFooter = haveFoot; } public RVBaseAdapter(Context context, List<T> beans, boolean haveHead) { mContext = context; mBeans = beans; mHaveHeader = haveHead; } public RVBaseAdapter(Context context, List<T> beans) { mContext = context; mBeans = beans; } @Override public RVBaseAdapter.CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(getItemLayoutID(viewType), parent, false); final CommonViewHolder holder = new CommonViewHolder(view); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClick(holder.getAdapterPosition()); } }); onCreateClickListener(holder, viewType); return holder; } @Override public void onBindViewHolder(RVBaseAdapter.CommonViewHolder holder, int position) { if(getItemViewType(position) == TYPE_HEADER){ onBindDataToView(holder, null); }else if(getItemViewType(position) == TYPE_FOOTER){ onBindDataToView(holder, null); }else{ if(mHaveHeader){ onBindDataToView(holder, mBeans.get(position-1)); }else{ onBindDataToView(holder, mBeans.get(position)); } } } /** * 为一些控件设置点击监听器 * @param holder */ protected abstract void onCreateClickListener(CommonViewHolder holder, int viewType); /** * 绑定数据到Item的控件中去 * * @param holder * @param bean */ protected abstract void onBindDataToView(CommonViewHolder holder, T bean); /** * 取得ItemView的布局文件 * * @return */ public abstract int getItemLayoutID(int viewType); @Override public int getItemCount() { int listSize = mBeans.size(); if(mHaveHeader){ listSize++; } if(mHaveFooter){ listSize++; } return listSize; } // @Override // public int getItemViewType(int position) { // if (position == 0 && mHaveHeader){ // //第一个item应该加载Header // return TYPE_HEADER; // } // if (position == getItemCount()-1 && mHaveFooter){ // //最后一个,应该加载Footer // return TYPE_FOOTER; // } // return TYPE_NORMAL; // } public void add(T bean) { mBeans.add(bean); notifyDataSetChanged(); } public void addAll(List<T> beans) { mBeans.addAll(beans); notifyDataSetChanged(); } public void clear() { mBeans.clear(); notifyDataSetChanged(); } public class CommonViewHolder extends RecyclerView.ViewHolder { private final SparseArray<View> mViews; private View itemView; public CommonViewHolder(View itemView) { super(itemView); this.mViews = new SparseArray<>(); this.itemView = itemView; } public View getItemView() { return itemView; } public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = itemView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public void setViewVisibility(int viewId, int visibilityType){ View view = getView(viewId); if(view != null){ view.setVisibility(visibilityType); } } public void setText(int viewId, String text) { TextView tv = getView(viewId); if(tv != null){ tv.setText(text); } } /** * 加载drawable中的图片 * * @param viewId * @param resId */ public void setImage(int viewId, int resId) { ImageView iv = getView(viewId); iv.setImageResource(resId); } /** * 加载网络上的图片 * * @param viewId * @param url */ public void setImageByGlide(int viewId, String url) { ImageView iv = getView(viewId); Glide.with(mContext) .load(url) .dontAnimate() .fitCenter() .diskCacheStrategy(DiskCacheStrategy.SOURCE) // .crossFade()//或者使用 dontAnimate() 关闭动画 .placeholder(R.drawable.picture_demo) .error(R.drawable.picture_demo) .into(iv); } public void setImageByImageLoader(int viewId, String url) { ImageView iv = getView(viewId); ImageLoader.getInstance().displayImage(url, iv); } } /** * ItemView的单击事件(如果需要,重写此方法就行) * * @param position */ protected abstract void onItemClick(int position); }