Android功能模块化之ListView中CheckBox批量操作

来源:互联网 发布:premiere cs6 mac下载 编辑:程序博客网 时间:2024/06/08 13:40

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/background_color"    android:orientation="horizontal"    android:paddingBottom="10dip"    android:paddingTop="10dip" >    <TextView        android:id="@+id/tv_time"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1.0"        android:gravity="center"        android:text="时间"        android:textColor="@color/black"        android:textSize="15sp" />    <TextView        android:id="@+id/tv_place"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1.5"        android:gravity="center"        android:text="地点"        android:textColor="@color/black"        android:textSize="15sp" />    <TextView        android:id="@+id/tv_money"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1.0"        android:gravity="center"        android:text="金额"        android:textColor="@color/black"        android:textSize="15sp" />    <CheckBox        android:id="@+id/cb_select"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_gravity="center|center_vertical"        android:layout_weight="0.5"        android:button="@drawable/cb_item_selected"        android:focusable="false"        android:focusableInTouchMode="false" /></LinearLayout>
2)定义ListView的Adapter,以下为一些关键的源代码

2.1)定义变量

private HashMap<Integer, Boolean> 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

@Overridepublic 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() {@Overridepublic 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<ViolationEntity> violationEntities,final Handler statusHandler) {this.context = context;isSelected = new HashMap<Integer, Boolean>();this.violationEntities = new ArrayList<ViolationEntity>();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() {@Overridepublic 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);
原创粉丝点击