安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)

ListView 自身提供了 CheckBox 只需要添加一行代码

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

但是这种实现想要自己控制操作起来局限很多。所以我选择了自己添加CheckBox的方式。可以支持列表项的全选,删除,并保持数据的对应关系不会乱。

列表中的CheckBox选中状态与一个Map进行绑定,利用adapter.notifyDataSetChanged();来更新界面。

效果如下:


安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)

下面直接看代码把。

main.xml

Java代码安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#C9F1FF">
  7. <ListView
  8. android:id="@id/android:list"
  9. android:layout_height="wrap_content"
  10. android:layout_width="fill_parent"
  11. android:fadingEdge="none"
  12. android:cacheColorHint="#00000000"/>
  13. <RelativeLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="40.0dip"
  16. android:layout_alignParentBottom="true">
  17. <CheckBoxandroid:id="@+id/all_check_btn"
  18. android:layout_width="40.0dip"
  19. android:background="@drawable/bottom_back_bg"
  20. android:layout_height="40.0dip"
  21. android:layout_alignParentLeft="true"/>
  22. </RelativeLayout>
  23. </RelativeLayout>

item.xml

Java代码安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="wrap_content"
  4. android:layout_marginRight="3.0dip"android:layout_weight="1.0"
  5. android:orientation="horizontal"android:descendantFocusability="blocksDescendants">
  6. <CheckBoxandroid:id="@+id/isCheakBox"android:layout_width="wrap_content"android:layout_height="wrap_content"
  7. android:layout_alignParentLeft="true"/>
  8. <!--日报图片-->
  9. <ImageViewandroid:id="@+id/dailyPic"android:contentDescription="dailyPic"
  10. android:layout_width="wrap_content"android:layout_height="wrap_content"
  11. android:layout_marginTop="3.0dip"android:src="@drawable/reports"
  12. android:layout_toRightOf="@id/isCheakBox"android:layout_centerVertical="true"/>
  13. <!--附件名称-->
  14. <TextView
  15. android:id="@+id/dailyName"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_toRightOf="@id/dailyPic"
  19. android:text="日报名称"android:layout_centerVertical="true"
  20. android:textColor="#000000"
  21. android:textSize="12.0sp"/>
  22. <ImageButtonandroid:id="@+id/deleteAttachment"
  23. android:layout_width="wrap_content"android:layout_height="wrap_content"
  24. android:layout_marginTop="3.0dip"android:background="@drawable/delete"
  25. android:layout_centerVertical="true"android:focusable="false"
  26. android:layout_alignParentRight="true"android:layout_marginRight="20dp"/>
  27. <!--附件名称-->
  28. </RelativeLayout>

Activity代码

Java代码安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
  1. publicclassListViewCheckBoxActivityextendsListActivity{
  2. privatestaticfinalStringTAG="ListViewCheckBoxActivity";
  3. privateList<Item>itemList;
  4. privateDraftDailyAdapteradapter;
  5. privateMap<Integer,Boolean>isCheckedMap;
  6. privateCheckBoxallCheckBox;
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. allCheckBox=(CheckBox)findViewById(R.id.all_check_btn);
  12. itemList=newArrayList<Item>();
  13. isCheckedMap=newHashMap<Integer,Boolean>();
  14. //初始化数据
  15. for(inti=0;i<8;i++){
  16. Itemitem=newItem();
  17. item.id=i;
  18. item.name="第"+i+"篇日报";
  19. itemList.add(item);
  20. isCheckedMap.put(i,false);
  21. }
  22. adapter=newDraftDailyAdapter(this,itemList);
  23. setListAdapter(adapter);
  24. allCheckBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  25. @Override
  26. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  27. Set<Integer>set=isCheckedMap.keySet();
  28. Iterator<Integer>iterator=set.iterator();
  29. if(isChecked){
  30. while(iterator.hasNext()){
  31. IntegerkeyId=iterator.next();
  32. isCheckedMap.put(keyId,true);
  33. }
  34. }else{
  35. while(iterator.hasNext()){
  36. IntegerkeyId=iterator.next();
  37. isCheckedMap.put(keyId,false);
  38. }
  39. }
  40. adapter.notifyDataSetChanged();
  41. }
  42. });
  43. }
  44. classDraftDailyAdapterextendsBaseAdapter{
  45. publicList<Item>list;
  46. privateContextcontext;
  47. LayoutInflaterinflater;
  48. publicDraftDailyAdapter(Contextcontext,List<Item>list){
  49. super();
  50. this.list=list;
  51. this.context=context;
  52. inflater=LayoutInflater.from(this.context);
  53. }
  54. @Override
  55. publicintgetCount(){
  56. returnlist==null?0:list.size();
  57. }
  58. @Override
  59. publicObjectgetItem(intlocation){
  60. returnlist.get(location);
  61. }
  62. @Override
  63. publiclonggetItemId(intposition){
  64. returnposition;
  65. }
  66. @Override
  67. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  68. ViewHolderholder=null;
  69. Itemitem=list.get(position);
  70. //Item的位置
  71. finalintlistPosition=position;
  72. //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态
  73. finalintid=item.id;
  74. if(convertView==null){
  75. holder=newViewHolder();
  76. convertView=inflater.inflate(R.layout.item,null);
  77. holder.tvName=(TextView)convertView.findViewById(R.id.dailyName);
  78. holder.deleteButton=(ImageButton)convertView.findViewById(R.id.deleteAttachment);
  79. holder.cBox=(CheckBox)convertView.findViewById(R.id.isCheakBox);
  80. convertView.setTag(holder);
  81. }else{
  82. holder=(ViewHolder)convertView.getTag();
  83. }
  84. Log.d(TAG,"id="+id);
  85. holder.cBox.setChecked(isCheckedMap.get(id));
  86. holder.tvName.setText(item.name);
  87. holder.deleteButton.setOnClickListener(newOnClickListener(){
  88. @Override
  89. publicvoidonClick(ViewparamView){
  90. //Log.d(TAG,"deletePosition="+listPosition+"");
  91. //删除list中的数据
  92. list.remove(listPosition);
  93. //删除Map中对应选中状态数据
  94. isCheckedMap.remove(id);
  95. //通知列表数据修改
  96. adapter.notifyDataSetChanged();
  97. }
  98. });
  99. holder.cBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
  100. @Override
  101. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  102. if(isChecked){
  103. isCheckedMap.put(id,true);
  104. }else{
  105. isCheckedMap.put(id,false);
  106. }
  107. }
  108. });
  109. returnconvertView;
  110. }
  111. publicfinalclassViewHolder{
  112. publicTextViewtvName;
  113. publicImageButtondeleteButton;
  114. publicCheckBoxcBox;
  115. }
  116. }
  117. classItem{
  118. privateIntegerid;
  119. privateStringname;
  120. }
  121. }

资源文件见附件源代码。