注意點:Listview的item中存在checkBox , radiobutton,會致使ListView.setOnItemClickListener無效。android
緣由: 事件會被子View捕獲到,ListView沒法捕獲處理該事件。ide
解決方法:在子view的xml中設置以下屬性:this
android:focusable="false"
android:clickable="false"xml
android:focusableInTouchMode="false"事件
案例:listview的多選get
listview.setAdapter(new MyAdapter4( xxxActivity.this, list) );it
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//設置爲多選
listview.setOnItemClickListener(itemClick_Listener); io
OnItemClickListener itemClick_Listener=new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {class
ViewHodler viewHodler=(ViewHodler) view.getTag(); / /省略了繁瑣的findviewbyid()操做
viewHodler.cb.toggle(); //清空checkBox的狀態
isCheck=viewHodler.cb.isChecked();
MyAdapter4.getIsSelected().put(position,isCheck);//記錄checkbox的選中狀態,在適配器再設置是否選中
if(isCheck){
//記錄選中後,實際操做
}
else{
//記錄未選中 cli
}
}
};
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集合中的值),而後提醒適配內容發生改變。