如何选择包含复选框的listview的所有行以及全选复选框

时间:2019-06-20 05:09:44

标签: android android-checkbox

我正在尝试将我的checkboxes的所有listview与另一个“全部选择” checkbox一起选择。我做了一些搜索,但没有一个起作用。当前的SELECT ALL chcekbox仅检查listview的最后一行,我需要它检查listview内的所有行。

这是我的getview方法

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;
        String betid = mData.get(i).get("id");
        ViewHolder holder ;
        if (mView == null) {
            Context context = viewGroup.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            mView = inflater.inflate(R.layout.row_layout, null,false);
            holder = new ViewHolder();
            holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
            holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
            holder.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
            selectAll = findViewById(R.id.selectAll);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (buttonView.isChecked()) {
                        checked.add((Integer) holder.checkBox.getTag());
                        holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
                    }
                    else {
                        holder.tx_amount.setBackgroundColor(Color.WHITE);
                        holder.tx_number.setBackgroundColor(Color.WHITE);
                        checked.remove((Integer) holder.checkBox.getTag());
                    }
                }
            });
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            selectAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for ( int i=0; i < listView.getChildCount(); i++) {
                        holder.checkBox.setChecked(true);
                    }
                }
            });
                mView.setTag(holder);
                holder.checkBox.setTag(i);
        } else {
            holder = (ViewHolder) mView.getTag();
            ((ViewHolder)mView.getTag()).checkBox.setTag(i);
        }
        if (betid != null) {
            String betnumber = mData.get(i).get("betnumber");
            String amountTarget = mData.get(i).get("betamount");
            holder.tx_amount.setText(amountTarget);
            holder.tx_number.setText(betnumber);
        }
        ViewHolder holde2r = (ViewHolder) mView.getTag();
        for (int k = 0; k < checked.size(); k++) {
            if (checked.get(k) == i) {
                holde2r.checkBox.setChecked(true);
            }
            else if (checked.get(k) != i) {
                holde2r.checkBox.setChecked(false);
            }
        }
        return mView;
    }

1 个答案:

答案 0 :(得分:1)

第1步:

在您的pojo类中添加boolean isSelected;(最初为false)

第2步:

onBindViewHolder()-> if(isSelected)->中将选中的true设置为您的复选框

第3步:

单击全选后->迭代您的数组列表( mData )并更改为

isSelected = true

并致电notifyDataSetChanged()