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

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

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

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

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

效果如下:


下面直接看代码把。

main.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns: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.         <CheckBox android: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代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <RelativeLayout xmlns: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.         <CheckBox android:id="@+id/isCheakBox" android:layout_width="wrap_content" android:layout_height="wrap_content"   
  7.             android:layout_alignParentLeft="true" />  
  8.         <!-- 日报图片 -->    
  9.         <ImageView android: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.          <ImageButton android: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代码  收藏代码
  1. public class ListViewCheckBoxActivity extends ListActivity {  
  2.     private static final String TAG = "ListViewCheckBoxActivity";  
  3.       
  4.     private List<Item> itemList;  
  5.     private DraftDailyAdapter adapter;  
  6.     private Map<Integer, Boolean> isCheckedMap;  
  7.     private CheckBox allCheckBox;  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         allCheckBox = (CheckBox)findViewById(R.id.all_check_btn);  
  13.         itemList = new ArrayList<Item>();  
  14.         isCheckedMap = new HashMap<Integer, Boolean>();  
  15.         //初始化数据  
  16.         for(int i=0;i<8;i++){  
  17.             Item item = new Item();  
  18.             item.id=i;  
  19.             item.name = "第"+i+"篇日报";  
  20.             itemList.add(item);  
  21.             isCheckedMap.put(i,false);  
  22.         }  
  23.           
  24.         adapter = new DraftDailyAdapter(this,itemList);  
  25.         setListAdapter(adapter);  
  26.         allCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){   
  27.             @Override   
  28.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
  29.                 Set<Integer> set = isCheckedMap.keySet();  
  30.                 Iterator<Integer> iterator = set.iterator();    
  31.                 if(isChecked){   
  32.                     while(iterator.hasNext()){     
  33.                         Integer keyId = iterator.next();     
  34.                         isCheckedMap.put(keyId,true);  
  35.                     }     
  36.                 }else{   
  37.                     while(iterator.hasNext()){     
  38.                         Integer keyId = iterator.next();    
  39.                         isCheckedMap.put(keyId,false);  
  40.                     }    
  41.                 }  
  42.                 adapter.notifyDataSetChanged();  
  43.             }   
  44.         });   
  45.     }  
  46.           
  47.     class DraftDailyAdapter extends BaseAdapter {  
  48.   
  49.         public List<Item> list;  
  50.         private Context context;  
  51.         LayoutInflater inflater;  
  52.   
  53.         public DraftDailyAdapter(Context context, List<Item> list) {  
  54.             super();  
  55.             this.list = list;  
  56.             this.context = context;  
  57.             inflater = LayoutInflater.from(this.context);  
  58.         }  
  59.         @Override  
  60.         public int getCount() {  
  61.             return list == null ? 0 : list.size();  
  62.         }  
  63.         @Override  
  64.         public Object getItem(int location) {  
  65.             return list.get(location);  
  66.         }  
  67.         @Override  
  68.         public long getItemId(int position) {  
  69.             return position;  
  70.         }  
  71.         @Override    
  72.         public View getView(int position, View convertView, ViewGroup parent) {    
  73.             ViewHolder holder = null;      
  74.             Item item = list.get(position);  
  75.             //Item的位置  
  76.             final int listPosition = position;  
  77.             //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态  
  78.             final int id = item.id;  
  79.             if(convertView == null){  
  80.                 holder = new ViewHolder();  
  81.                 convertView = inflater.inflate(R.layout.item, null);    
  82.                 holder.tvName = (TextView)convertView.findViewById(R.id.dailyName);    
  83.                 holder.deleteButton = (ImageButton)convertView.findViewById(R.id.deleteAttachment);  
  84.                 holder.cBox = (CheckBox)convertView.findViewById(R.id.isCheakBox);  
  85.                 convertView.setTag(holder);  
  86.             }else{  
  87.                 holder = (ViewHolder) convertView.getTag();  
  88.             }  
  89.             Log.d(TAG, "id="+id);  
  90.             holder.cBox.setChecked(isCheckedMap.get(id));  
  91.             holder.tvName.setText(item.name);   
  92.             holder.deleteButton.setOnClickListener(new OnClickListener() {  
  93.                 @Override  
  94.                 public void onClick(View paramView) {  
  95.                     //Log.d(TAG, "deletePosition="+listPosition+"");  
  96.                     //删除list中的数据  
  97.                     list.remove(listPosition);  
  98.                     //删除Map中对应选中状态数据  
  99.                     isCheckedMap.remove(id);  
  100.                     //通知列表数据修改  
  101.                     adapter.notifyDataSetChanged();  
  102.                 }  
  103.             });  
  104.             holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){   
  105.                 @Override   
  106.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
  107.                     if(isChecked){   
  108.                         isCheckedMap.put(id,true);  
  109.                     }else{   
  110.                         isCheckedMap.put(id,false);  
  111.                     }  
  112.                 }   
  113.             });   
  114.             return convertView;    
  115.         }  
  116.         public final class ViewHolder {      
  117.             public TextView tvName;      
  118.             public ImageButton deleteButton;      
  119.             public CheckBox cBox;      
  120.         }      
  121.     }  
  122.   
  123.     class Item {  
  124.         private Integer id;  
  125.         private String name;  
  126.     }  
  127.       
  128. }  

资源文件见附件源代码。

原文地址:https://www.cnblogs.com/xiaochao1234/p/4233511.html