ListView加载CheckBox,在进行全选操作时,或全选状态下,勾选ListView中Item的CheckBox时,全选状态没有改变之类的情况。
ListView中itemCheckBox与全选AllCheckBox存在以下关联:
(1)AllCheckBox选中状态与未选中状态下,itemCheckBox随之变化;
(2)itemCheckBox未选中时,应AllCheckBox为未选中状态;
(3)itemCheckBox选中时,需判断ListView中所有的checkbox是否处于选中状态,若为选中状态,则将AllCheckBox状态改为选中,否则为未选中状态。
解决思路:
(1)在Adapter存下每个checkBox的状态;
(2)通过AllCheckBox状态改变itemCheckBox状态;
(3)通过判断Adapter中所有checkbox的选中状态,去更新AllCheckBox状态;
(4)使用Handler更新AllCheckBox状态。
实现代码:
1)定义ListView的item.xml
2)定义ListView的Adapter,以下为一些关键的源代码
2.1)定义变量
private HashMap isSelected = null;//存储checkbox状态
private Handler statusHandler = null;//更新AllCheckBox的handler
private class ViewHolder {TextView tvTime, tvPlace, tvMoney;CheckBox cbSelect;}
2.2)定义判断全选状态的方法
private boolean isAllSelected() {
for (int i = 0; i < isSelected.size(); i++) {
if (!isSelected.get(i)) {
return false;
}
}
return true;
}
2.3)定义选择checkBox方法
public void select(int potision, boolean isChecked) {
isSelected.put(potision, isChecked);//记录下当前checkBox的状态
Message msg = statusHandler.obtainMessage();
if (isChecked) {
if (isAllSelected()) {
msg.what = MainActivity.ALL_SELECTED_CHECK;//将AllCheckbox状态改为选中
} else {
msg.what = MainActivity.NOT_ALL_SELECTED_CHECK;//将AllCheckBox改成未选中
}
} else {
msg.what = MainActivity.NOT_ALL_SELECTED_CHECK;//将AllCheckBox改成未选中
}
statusHandler.sendMessage(msg);
}
2.4)定义getView
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.list_item_vios, null);
viewHolder = new ViewHolder();
viewHolder.tvMoney = (TextView) convertView
.findViewById(R.id.tv_money);
viewHolder.tvPlace = (TextView) convertView
.findViewById(R.id.tv_place);
viewHolder.tvTime = (TextView) convertView
.findViewById(R.id.tv_time);
viewHolder.cbSelect = (CheckBox) convertView
.findViewById(R.id.cb_select);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.cbSelect
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
select(position, isChecked);
}
});
Boolean isChecked = isSelected.get(position);
if (null == isChecked) {
isChecked = false;
}
ViolationEntity violationEntity = violationEntities.get(position);
viewHolder.tvMoney.setText(violationEntity.getBenjin());
viewHolder.tvPlace.setText(violationEntity.getWfdd());
viewHolder.tvTime.setText(violationEntity.getWfsj());
viewHolder.cbSelect.setChecked(isChecked);
return convertView;
}
2.5)定义Adapter构造函数
public ViosAdapter(Context context,
ArrayList violationEntities,
final Handler statusHandler) {
this.context = context;
isSelected = new HashMap();
this.violationEntities = new ArrayList();
this.violationEntities = violationEntities;
this.statusHandler = statusHandler;
initSelectAll(false);//初始化全部checkBox的状态
}
3)定义MainActivity中AllCheckBox相应操作
3.1)定义handler常量
public static final int ALL_SELECTED_CHECK = 0x0000;//更新AllCheckBox为选中
public static final int NOT_ALL_SELECTED_CHECK = 0x0001;//更新AllCheckBox为未选中
public static final int UPDATE_ALL_CHECKBOX = 0x0002;//更新itemCheckBox状态
private boolean updateAllCheckBox = true;//区分是AllCheckBox点击改变状态还是由itemCheckBox改变AllCheckBox状态
3.2)定义AllCheckBox监听函数
cbAllSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (updateAllCheckBox) {//如果是点击时,则更新Adapter中所有checkBox的状态
Message msg = statusHandler.obtainMessage();
msg.what = UPDATE_ALL_CHECKBOX;
msg.obj = isChecked;
statusHandler.sendMessage(msg);
}
}
});
4)定义更新数据的Handler
private Handler statusHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case ALL_SELECTED_CHECK:
updateAllCheckBox = false;
cbAllSelected.setChecked(true);
updateAllCheckBox = true;
break;
case NOT_ALL_SELECTED_CHECK:
updateAllCheckBox = false;
cbAllSelected.setChecked(false);
updateAllCheckBox = true;
break;
case UPDATE_ALL_CHECKBOX:
((ViosAdapter) lvVios.getAdapter()).selectAll(Boolean
.parseBoolean(String.valueOf(msg.obj)));
break;
default:
break;
}
};
};
5)初始化Adapter,ListView加载Adapter
lvVios = (ListView) findViewById(R.id.ll_vios);
viosAdapter = new ViosAdapter(this,
violationEntities, statusHandler);
lvVios.setAdapter(viosAdapter);