qq_41771258的博客

小小安卓

RecycleView中CheckBox全选、反选、单选

思路:首先需要一个Map集合用于存储每一个条目的选中状态。选中哪个条目,则把该条目的位置添加进集合,并设置key为true,删除则设置Map集合中对应的位置的key为false。最后提交的时候,遍历map集合,即可得到所有选中的条目。全选是把Map集合中的key全部设置为true即可,而反选则需要遍历Map集合,判断每个key的状态并设置为相反的状态。

先上适配器的代码

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    /**

     * checkbox的Hashmap集合,一条数据对应一个checkbox状态

     */
    private final HashMap<Integer, Boolean> map;


    /**
     *数据源
     */
    private final List<String> list;


    public MyAdapter() {
        map = new HashMap<>();
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("这是条目" + i);
            //Checkbox初始状态置为false
            map.put(i, false);
        }
    }


    /**
     * 全选,
     */
    public void selectAll() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        boolean shouldall = false;
        for (Map.Entry<Integer, Boolean> entry : entries) {
            Boolean value = entry.getValue();
            if (!value) {
                shouldall = true;
                break;
            }
        }


        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(shouldall);
        }
        notifyDataSetChanged();
    }


    /**
     * 反选
     */
    public void neverAll() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(!entry.getValue());
        }
        notifyDataSetChanged();
    }


    /**
     * 单选
     * @param postion
     */
    public void singleSelect(int postion) {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(false);
        }
        map.put(postion, true);
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.check, parent, false);
        return new MyViewHolder(inflate);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.txt.setText(list.get(position));
        //从map集合获取状态
        holder.checkBox.setChecked(map.get(position));


        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                map.put(position, !map.get(position));
                //刷新适配器
                notifyDataSetChanged();
                //单选
                singleSelect(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list!=null ? list.size():0;
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        private TextView txt;
        private CheckBox checkBox;


        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            txt = (TextView) itemView.findViewById(R.id.txt);
            checkBox = (CheckBox) itemView.findViewById(R.id.cbox);
        }
    }
}


Activity的代码:

public class MainActivity extends AppCompatActivity {


    private MyAdapter myAdapter;
    private LinearLayoutManager linearLayoutManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);


        //在加载数据之前配置
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);


        //创建一个适配器
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
    }


    /**
     * 全选
     * @param view
     */
    public void btnAll(View view) {
        myAdapter.selectAll();
    }


    /**
     * 单选
     * @param view
     */
    public void btnner(View view) {
        myAdapter.neverAll();
    }
}

阅读更多
下一篇仿QQ上传头像
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭