Listview中checkBoxe的多选

原创
2015/08/03 10:32
阅读数 192

注意点:Listview的item中存在checkBox , radiobutton,会导致ListView.setOnItemClickListener无效。

原因:    事件会被子View捕获到,ListView无法捕获处理该事件。

解决方法:在子view的xml中设置如下属性:

                 android:focusable="false"
                 android:clickable="false"

                 android:focusableInTouchMode="false"

 

案例:listview的多选

 listview.setAdapter(new MyAdapter4(  xxxActivity.this, list) );

 listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//设置为多选
 listview.setOnItemClickListener(itemClick_Listener);  

 OnItemClickListener  itemClick_Listener=new OnItemClickListener() {
       public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {


                ViewHodler viewHodler=(ViewHodler) view.getTag();     / /省略了繁琐的findviewbyid()操作
                 viewHodler.cb.toggle();                                              //清空checkBox的状态
                 isCheck=viewHodler.cb.isChecked();
                 MyAdapter4.getIsSelected().put(position,isCheck);//记录checkbox的选中状态,在适配器再设置是否选中
                   if(isCheck){
                                 //记录选中后,实际操作
                   }
                  else{
                                   //记录未选中         

                  }

          }
 };

 

 

public class MyAdapter4  extends BaseAdapter {
 Context context;
 List<FixedInfo> list;
 private static HashMap<Integer , Boolean> isSelected=new HashMap<Integer, Boolean>();

 public MyAdapter4(Context context,List<FixedInfo> list) {
                this.context=context;
                this.list=list;
     
                  initMap();
 }
    
 public void initMap(){
     for(int i=0;i<list.size();++i){
               getIsSelected().put(i, false);
     }
     
  }
 
 @Override
 public int getCount() {
             return list.size();
 }

 @Override
 public Object getItem(int arg0) {
               return list.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
              return arg0;
 }

 @Override
 public View getView(int position, View contentview, ViewGroup arg2) {
                 ViewHodler viewHodler;
                  if(contentview==null){
                           viewHodler=new ViewHodler();
                           contentview=View.inflate(context, R.layout.checkboxelinearlayout, null);
                            viewHodler.cb=(CheckBox) contentview.findViewById(R.id.checkboxelinearlayout_checkBox1);
                             viewHodler.text1=(TextView) contentview.findViewById(R.id.checkboxelinearlayout_xulie);
                              viewHodler.text2=(TextView) contentview.findViewById(R.id.checkboxelinearlayout_number);
                               viewHodler.text3=(TextView) contentview.findViewById(R.id.checkboxelinearlayout_name);
                               contentview.setTag(viewHodler);
                       }
                      else{
                          viewHodler=(ViewHodler) contentview.getTag();
                       }
                      viewHodler.text1.setText(position+1+"");
                      viewHodler.cb.setChecked( getIsSelected().get(position) );      //根据状态来设置是否选中
                      viewHodler.text2.setText(list.get(position).Worker_Number);
                      viewHodler.text2.setText(list.get(position).Worker_Name);
                      return contentview;
 }
 
 public static  HashMap<Integer, Boolean> getIsSelected() {
                     return isSelected;
 }
 public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
                     MyAdapter4.isSelected = isSelected;
 }


}

 

 

子view的xml  : 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <CheckBox
        android:id="@+id/checkboxelinearlayout_checkBox1"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>
    <TextView
        android:id="@+id/checkboxelinearlayout_xulie"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="TextView" />
    </LinearLayout>
 

    <TextView
        android:id="@+id/checkboxelinearlayout_number"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/checkboxelinearlayout_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

 

衍生点:

全选,反选等操作,只需在对应按钮的事件中,设置checkbox的状态(即设置map集合中的值),然后提醒适配内容发生改变。

 

 

 

展开阅读全文
打赏
0
7 收藏
分享
加载中
更多评论
打赏
0 评论
7 收藏
0
分享
返回顶部
顶部