recyclerview item 单选

1. 代码中有butterknife在adapter里面的使用

2.单选主要用的是SparseBooleanArray 进行数据保存

public class MainAdapter extends RecyclerView.Adapter {

    //
    private final ArrayList mDatas;
    private Context context;
    private RecyclerView rv;
    /**
     * SparseBooleanArray 存放boolean 类型的pair(key,value)
     */
    private SparseBooleanArray mSelectArray;

    public MainAdapter(Context context, RecyclerView rv) {
        this.context = context;
        this.rv = rv;
        mDatas = new ArrayList();
        for (int i = 0; i < 30; i++) {
            ItemBean bean = new ItemBean();
            bean.setMsg("数据" + (char) i);
            mDatas.add(bean);
        }
        mSelectArray = new SparseBooleanArray();
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MainViewHolder holder = new MainViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item1, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MainViewHolder holder, final int position) {
        holder.tvCoupon.setText(mDatas.get(position).getMsg());
        holder.ivSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSelectArray.clear();
                mSelectArray.put(position, holder.ivSelect.isChecked());
                notifyDataSetChanged();
            }
        });
        holder.ivSelect.setChecked(mSelectArray.get(position, false));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void removeData(int position) {
        mDatas.remove(position);
        //  通知RecyclerView控件某个Item已经被删除
        notifyItemRemoved(position);
    }

    public void addData(int position) {
        ItemBean bean = new ItemBean();
        bean.setMsg("add");
        mDatas.add(bean);
        notifyItemInserted(position);
    }

    static class MainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivSelect)
        CheckBox ivSelect;
        @BindView(R.id.tvCoupon)
        TextView tvCoupon;
        @BindView(R.id.tvRight)
        TextView tvRight;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public SparseBooleanArray getmSelectArray() {
        return mSelectArray;
    }

}


item1.xml

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="#efefef"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center"
    android:orientation="horizontal">

            android:id="@+id/ivSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:focusable="false"
        android:button="@null"
        android:background="@drawable/cb_bg2"/>

            android:id="@+id/tvCoupon"
        android:layout_width="0dp"
        android:layout_height="140dp"
        android:layout_marginLeft="15dp"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:orientation="vertical"
        android:text="¥50"
        android:textColor="@android:color/white"
        android:textSize="20sp">
   

            android:id="@+id/tvRight"
        android:layout_width="0dp"
        android:layout_height="140dp"
        android:layout_marginRight="15dp"
        android:layout_weight="2"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="我是优惠券"
        android:textColor="@android:color/white"
        android:textSize="14sp">
   



2. cb_bg2.xml


   
   



以后可以参考的资料:

http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650237708&idx=1&sn=06c24230d4c03310e753bc07d9ae6fb8&chksm=88639a63bf141375670d1bb1dbd3d467c86fd4231116b7658b54af04aaec960dfa3047e7a8e1&scene=4#wechat_redirect

你可能感兴趣的:(recyclerview item 单选)