//基础使用类 public class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> { protected OnRecyclerViewClickListener mOnClickListener; protected Context mContext; protected LayoutInflater mInflater; protected BaseRecyclerAdapter(Context context, List<T> list) { this.mContext = context; this.list = list; this.mInflater = LayoutInflater.from(mContext); } protected List<T> list; private boolean isShowChecked = false; private List<T> checkedList = new ArrayList<T>(); @Override public VH onCreateViewHolder(ViewGroup parent, int viewType) { return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) { //用于局部刷新 } @Override public int getItemCount() { return list == null ? 0 : list.size(); } /** * 点击Item选中复选框 * * @param t */ protected void changeCheckedItem(T t) { if (isShowChecked) { if (checkedList.contains(t)) { checkedList.remove(t); } else { checkedList.add(t); } notifyDataSetChanged(); } } /** * 设置默认的选中值 */ protected void setChecked(T t) { checkedList.clear(); checkedList.add(t); notifyDataSetChanged(); } /** * 显示复选框 * * @param isShowChecked */ public void setShowChecked(boolean isShowChecked) { this.isShowChecked = isShowChecked; clearCheckedAll(); } /** * 复选框是否显示 * * @return */ public boolean isShowChecked() { return isShowChecked; } /** * 全选 */ public void addCheckedAll() { checkedList.clear(); checkedList.addAll(list); notifyDataSetChanged(); } /** * 取消全选 */ public void clearCheckedAll() { checkedList.clear(); notifyDataSetChanged(); } /** * 返回所有的选中对象 * * @return */ public List<T> getCheckedList() { return checkedList; } public void setOnClickListener(OnRecyclerViewClickListener onClickListener) { this.mOnClickListener = onClickListener; } public interface OnRecyclerViewClickListener { /** * 点击item监听时间 * * @param view View * @param position position */ void onItemClick(View view, int position); /** * 长按监听时间 * * @param view View * @param position position */ void onItemLongClick(View view, int position); } }
使用方法
public class DownloadAdapter extends BaseRecyclerAdapter<DownloadEntity, DownloadAdapter.ViewHolder> implements View.OnClickListener { private final int layoutId = R.layout.download_item; private OnAdapterListener onAdapterListener; public DownloadAdapter(Context context, List<DownloadEntity> list) { super(context, list); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(layoutId, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position, List<Object> payloads) { DownloadAdapter.ViewHolder holder = (DownloadAdapter.ViewHolder) viewHolder; final DownloadEntity downloadEntity = list.get(position); if (payloads.isEmpty()) { //全部刷新 holder.itemView.setTag(layoutId, position); if (isShowChecked()) { holder.cbxChecked.setVisibility(View.VISIBLE); } else { holder.cbxChecked.setVisibility(View.GONE); } holder.cbxChecked.setChecked(getCheckedList().contains(downloadEntity)); holder.cbxChecked.setTag(layoutId, position); holder.cbxChecked.setOnClickListener(this); responseUIListener(downloadEntity, holder); } else { //局部刷新 responseUIListener(downloadEntity, holder); } } @Override public void onClick(View v) { int position = (int) v.getTag(layoutId); DownloadEntity downloadEntity = list.get(position); switch (v.getId()) { case R.id.cbx_checked: changeCheckedItem(downloadEntity); break; default: if (isShowChecked()) { changeCheckedItem(downloadEntity); } else { if (onAdapterListener != null) { onAdapterListener.onItemClick(v, downloadEntity); } } break; } } class ViewHolder extends RecyclerView.ViewHolder { TextView title; TextView content; CornerImageView avatar; TextView state; TextView control; CheckBox cbxChecked; public ViewHolder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.tv_title); content = (TextView) itemView.findViewById(R.id.tv_content); avatar = (CornerImageView) itemView.findViewById(R.id.img_avatar); state = (TextView) itemView.findViewById(R.id.tv_state); control = (TextView) itemView.findViewById(R.id.tv_control); cbxChecked = (CheckBox) itemView.findViewById(R.id.cbx_checked); } } public void setOnAdapterListener(OnAdapterListener onAdapterListener) { this.onAdapterListener = onAdapterListener; } public interface OnAdapterListener { void onItemClick(View v, final DownloadEntity downloadEntity); void onItemProgressClick(View view, int position, final DownloadEntity downloadEntity); }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:gravity="center_vertical" android:orientation="vertical"> <RelativeLayout android:id="@+id/relayt_image" style="@style/ItemSecond" android:clickable="false"> <CheckBox android:id="@+id/cbx_checked" android:layout_width="18dp" android:layout_height="18dp" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/margin_18" android:button="@drawable/cm_sel_checkbox_gray" /> <ImageView android:id="@+id/img_avatar" android:layout_width="36dp" android:layout_height="36dp" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/margin_18" android:layout_toEndOf="@+id/cbx_checked" android:scaleType="fitXY" android:src="@drawable/cloud_icon_folder" /> <RelativeLayout android:id="@+id/relayt_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/margin_12" android:layout_toEndOf="@+id/img_avatar" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/margin_80" android:layout_marginTop="8dp" android:layout_toStartOf="@+id/img_more" android:ellipsize="middle" android:maxLines="1" android:text="IMG_00000001.png" android:textColor="#000000" android:textSize="15dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginRight="@dimen/margin_80" android:layout_marginTop="8dp" android:layout_toStartOf="@+id/img_more" android:orientation="horizontal" android:paddingBottom="8dp"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_2" android:ellipsize="middle" android:maxLines="1" android:text="0MB/0MB" android:textColor="#93999d" android:textSize="11dp" /> <TextView android:id="@+id/tv_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_10" android:ellipsize="middle" android:maxLines="1" android:text="" android:textColor="#93999d" android:textSize="11dp" /> </LinearLayout> <TextView android:id="@+id/tv_control" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="11dp" android:background="@color/transparent" android:clickable="true" android:padding="5dp" android:visibility="visible" /> </RelativeLayout> <View android:id="@+id/vline_short" android:layout_width="match_parent" android:layout_height="1px" android:layout_alignParentBottom="true" android:layout_alignStart="@+id/relayt_content" android:layout_gravity="bottom" android:background="#dedede" /> <View android:id="@+id/vline_long" android:layout_width="match_parent" android:layout_height="1px" android:layout_alignParentBottom="true" android:layout_gravity="bottom" android:background="#dedede" /> </RelativeLayout> </LinearLayout>
是否显示 多选框
boolean showChecked = !mDownloadAdapter.isShowChecked(); mDownloadAdapter.setShowChecked(showChecked);
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_download); mDownloadAdapter = new DownloadAdapter(mContext, downloadEntities); mRecyclerView.setAdapter(mDownloadAdapter); mDownloadAdapter.notifyDataSetChanged(); mDownloadAdapter.setOnAdapterListener(this);