listview中带有checkbox实现多选操作

关于listview中带有checkbox实现多选操作,我都接触好多次了,但是每次要想起来实现的逻辑,还得郁闷半天,今天特意记录一下哈。
虽然现在recyclerview已经替代listview,但是两者的实现方式都是相同的,新的工作就是填坑的,所以只能在之前的基础上修改
1.     首先声明一个实体类,在类里面添加isChecked标识,默认false
2.     布局文件:
        checkbox跟listview的item会存在点击事件冲突,所以设置checkbox为
                android:focusable="false"
                android:clickable="false"
3.      adapter
 //对adapter添加checkbox点击监听接口
    public static interface  OnCheckClickListener{
        void onItemClick(View view, int position);
    }
    private BlackListAdapter.OnCheckClickListener mOnItemClickListener;
    public void setOnItemCheckListener(BlackListAdapter.OnCheckClickListener listener) {
        this.mOnItemClickListener = listener;
    }
checkbox的点击事件:
  holder.del_Cbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //  Log.i("info","position打印"+position+"select:"+allTagList.get(section).tagInfoList.get(position).isSelected());
                        mBlacks.get((Integer)(finalHolder).del_Cbox.getTag()).setCheckedFlag(!mBlacks.get((Integer)(finalHolder).del_Cbox.getTag()).isCheckedFlag());
                        Log.i("info","position打印"+(Integer)(finalHolder).del_Cbox.getTag());
                        notifyDataSetChanged();
                        mOnItemClickListener.onItemClick(finalHolder.del_Cbox,(Integer)(finalHolder).del_Cbox.getTag());
                        }
                });

更新集合数据:
 /**
     * 更新集合数据
     * @param context
     * @param blacks
     */
    public void setData(Context context,List blacks){
                this.mBlacks = blacks;
                editorFlag = SharedPreferencesUtil.getStringData(context, ConstantDescUtils.BLACK_EDIT_FLAG,"");
                Log.i("info","mBlackListAdapter-->editorFlag1测试");
                notifyDataSetChanged();
    }
})
4.activity里面
     给adapter设置点击事件,获取相对应的信息
 mBlackListAdapter.setOnItemCheckListener(new BlackListAdapter.OnCheckClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.i("info","mBlackListAdapter--->"+position);
                CheckBox checkBox = (CheckBox) view;
                mBlacks.get(position).setCheckedFlag(checkBox.isChecked());
                buildDelDialog(BlackListActivity.this,position);
                Log.i("info","mBlackListAdapter--->isChecked"+checkBox.isChecked());
            }
        });
        mBlackListAdapter.notifyDataSetChanged();


你可能感兴趣的:(recyclerview)