我正在尝试将我的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;
}
答案 0 :(得分:1)
第1步:
在您的pojo
类中添加boolean isSelected;
(最初为false)
第2步:
在onBindViewHolder()
-> if(isSelected)
->中将选中的true
设置为您的复选框
第3步:
单击全选后->迭代您的数组列表( mData )并更改为
isSelected = true
并致电notifyDataSetChanged()