自己封装的 recyclerView的 BaseAdapter,带item点击和长按事件。
第一种是把创建viewholder的步骤给子类来做。
第二种是参照陈宇明的博客,试着只暴露给子类设置holder属性的方法。(还未测试条目中控件的点击事件能否成功)
代码如下:
/** * Created by star on 2016/7/28 0028. * */ public abstract class MyBaseRecAdapter<T extends RecyclerView.ViewHolder,E> extends RecyclerView.Adapter<T> { private static OnItemClickListener onItemClickListener; public static interface OnItemClickListener<E> { void onItemClick(View view, E bean); void onItemLongClick(View view, E bean); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } protected Context context; protected ArrayList<E> datas; protected int layoutID; public MyBaseRecAdapter(Context context, ArrayList<E> datas, int layoutID) { this.context = context; this.datas = datas; this.layoutID=layoutID; } @Override public T onCreateViewHolder(ViewGroup parent, int viewType){ View itemView=View.inflate(context,layoutID,null); return getViewHolder(itemView); } @Override public int getItemCount() { return datas.size(); } @Override public void onBindViewHolder(final T holder, final int position){ final E bean = datas.get(position); setValues(holder,bean); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(holder.itemView, bean); } } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemLongClick(holder.itemView, bean); } return true; } }); } /** * 返回viewholder * @param itemView * @return */ protected abstract T getViewHolder(View itemView); /** * 设置控件数据 * @param holder * @param bean */ protected abstract void setValues(T holder, E bean); }
第二种,待完善:
/** * Created by star on 2016/7/28 0028. * */ public abstract class MyBaseAdapter_2<E> extends RecyclerView.Adapter<MyBaseAdapter_2.BaseViewHolder> { private static OnItemClickListener onItemClickListener; public static interface OnItemClickListener<E> { void onItemClick(View view, E bean); void onItemLongClick(View view, E bean); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } protected Context context; protected ArrayList<E> datas; protected int layoutID; public MyBaseAdapter_2(Context context, ArrayList<E> datas, int layoutID) { this.context = context; this.datas = datas; this.layoutID = layoutID; } @Override public MyBaseAdapter_2.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = View.inflate(context, layoutID, null); return new BaseViewHolder(context, itemView); } @Override public int getItemCount() { return datas.size(); } @Override public void onBindViewHolder(final MyBaseAdapter_2.BaseViewHolder holder, final int position) { final E bean = datas.get(position); setValues(holder, bean); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(holder.itemView, bean); } } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemLongClick(holder.itemView, bean); } return true; } }); } /** * 设置控件数据 * * @param holder * @param bean */ protected abstract void setValues(MyBaseAdapter_2.BaseViewHolder holder, E bean); static class BaseViewHolder extends RecyclerView.ViewHolder { private SparseArray<View> views; private Context context; public BaseViewHolder(Context context, View itemView) { super(itemView); this.context = context; views = new SparseArray<>(); } protected <T extends View> T inflateView(int viewId) { View view = views.get(viewId); if (view == null) { view = itemView.findViewById(viewId); views.put(viewId,view); } return (T) view; } public BaseViewHolder setText(int viewId, CharSequence value) { TextView tv = inflateView(viewId); tv.setText(value); return this; } /** * 通过图片地址加载图片 * @param viewId * @param imgUrl * @return */ public BaseViewHolder setImgUrl(int viewId, String imgUrl) { ImageView img = inflateView(viewId); Glide.with(context).load(imgUrl).into(img); return this; } /** * 通过项目drawable中加载图片 * @param viewId * @param imgResourceId * @return */ public BaseViewHolder setImgUrl(int viewId, int imgResourceId) { ImageView img = inflateView(viewId); Glide.with(context).load(imgResourceId).into(img); return this; } public BaseViewHolder setOther(int viewId){ inflateView(viewId); return this; } public View getView(int viewId){ return views.get(viewId); } } }