安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
ListView 自身提供了 CheckBox 只需要添加一行代码
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
但是这种实现想要自己控制操作起来局限很多。所以我选择了自己添加CheckBox的方式。可以支持列表项的全选,删除,并保持数据的对应关系不会乱。
列表中的CheckBox选中状态与一个Map进行绑定,利用adapter.notifyDataSetChanged();来更新界面。
效果如下:
下面直接看代码把。
main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#C9F1FF">
- <ListView
- android:id="@id/android:list"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:fadingEdge="none"
- android:cacheColorHint="#00000000"/>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="40.0dip"
- android:layout_alignParentBottom="true">
- <CheckBoxandroid:id="@+id/all_check_btn"
- android:layout_width="40.0dip"
- android:background="@drawable/bottom_back_bg"
- android:layout_height="40.0dip"
- android:layout_alignParentLeft="true"/>
- </RelativeLayout>
- </RelativeLayout>
item.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"android:layout_height="wrap_content"
- android:layout_marginRight="3.0dip"android:layout_weight="1.0"
- android:orientation="horizontal"android:descendantFocusability="blocksDescendants">
- <CheckBoxandroid:id="@+id/isCheakBox"android:layout_width="wrap_content"android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <!--日报图片-->
- <ImageViewandroid:id="@+id/dailyPic"android:contentDescription="dailyPic"
- android:layout_width="wrap_content"android:layout_height="wrap_content"
- android:layout_marginTop="3.0dip"android:src="@drawable/reports"
- android:layout_toRightOf="@id/isCheakBox"android:layout_centerVertical="true"/>
- <!--附件名称-->
- <TextView
- android:id="@+id/dailyName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/dailyPic"
- android:text="日报名称"android:layout_centerVertical="true"
- android:textColor="#000000"
- android:textSize="12.0sp"/>
- <ImageButtonandroid:id="@+id/deleteAttachment"
- android:layout_width="wrap_content"android:layout_height="wrap_content"
- android:layout_marginTop="3.0dip"android:background="@drawable/delete"
- android:layout_centerVertical="true"android:focusable="false"
- android:layout_alignParentRight="true"android:layout_marginRight="20dp"/>
- <!--附件名称-->
- </RelativeLayout>
Activity代码
- publicclassListViewCheckBoxActivityextendsListActivity{
- privatestaticfinalStringTAG="ListViewCheckBoxActivity";
- privateList<Item>itemList;
- privateDraftDailyAdapteradapter;
- privateMap<Integer,Boolean>isCheckedMap;
- privateCheckBoxallCheckBox;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- allCheckBox=(CheckBox)findViewById(R.id.all_check_btn);
- itemList=newArrayList<Item>();
- isCheckedMap=newHashMap<Integer,Boolean>();
- //初始化数据
- for(inti=0;i<8;i++){
- Itemitem=newItem();
- item.id=i;
- item.name="第"+i+"篇日报";
- itemList.add(item);
- isCheckedMap.put(i,false);
- }
- adapter=newDraftDailyAdapter(this,itemList);
- setListAdapter(adapter);
- allCheckBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
- @Override
- publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
- Set<Integer>set=isCheckedMap.keySet();
- Iterator<Integer>iterator=set.iterator();
- if(isChecked){
- while(iterator.hasNext()){
- IntegerkeyId=iterator.next();
- isCheckedMap.put(keyId,true);
- }
- }else{
- while(iterator.hasNext()){
- IntegerkeyId=iterator.next();
- isCheckedMap.put(keyId,false);
- }
- }
- adapter.notifyDataSetChanged();
- }
- });
- }
- classDraftDailyAdapterextendsBaseAdapter{
- publicList<Item>list;
- privateContextcontext;
- LayoutInflaterinflater;
- publicDraftDailyAdapter(Contextcontext,List<Item>list){
- super();
- this.list=list;
- this.context=context;
- inflater=LayoutInflater.from(this.context);
- }
- @Override
- publicintgetCount(){
- returnlist==null?0:list.size();
- }
- @Override
- publicObjectgetItem(intlocation){
- returnlist.get(location);
- }
- @Override
- publiclonggetItemId(intposition){
- returnposition;
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- ViewHolderholder=null;
- Itemitem=list.get(position);
- //Item的位置
- finalintlistPosition=position;
- //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态
- finalintid=item.id;
- if(convertView==null){
- holder=newViewHolder();
- convertView=inflater.inflate(R.layout.item,null);
- holder.tvName=(TextView)convertView.findViewById(R.id.dailyName);
- holder.deleteButton=(ImageButton)convertView.findViewById(R.id.deleteAttachment);
- holder.cBox=(CheckBox)convertView.findViewById(R.id.isCheakBox);
- convertView.setTag(holder);
- }else{
- holder=(ViewHolder)convertView.getTag();
- }
- Log.d(TAG,"id="+id);
- holder.cBox.setChecked(isCheckedMap.get(id));
- holder.tvName.setText(item.name);
- holder.deleteButton.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(ViewparamView){
- //Log.d(TAG,"deletePosition="+listPosition+"");
- //删除list中的数据
- list.remove(listPosition);
- //删除Map中对应选中状态数据
- isCheckedMap.remove(id);
- //通知列表数据修改
- adapter.notifyDataSetChanged();
- }
- });
- holder.cBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
- @Override
- publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
- if(isChecked){
- isCheckedMap.put(id,true);
- }else{
- isCheckedMap.put(id,false);
- }
- }
- });
- returnconvertView;
- }
- publicfinalclassViewHolder{
- publicTextViewtvName;
- publicImageButtondeleteButton;
- publicCheckBoxcBox;
- }
- }
- classItem{
- privateIntegerid;
- privateStringname;
- }
- }
资源文件见附件源代码。