<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_vertical" android:padding="5dp" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:focusable="false" android:text="" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="74dp" android:layout_toRightOf="@+id/checkBox1" android:text="" /> </RelativeLayout>
android:focusable="false"这样事件才能传递到List view 的item。
private ListAdapter adapter = new ListAdapter(){ @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public boolean hasStableIds() { return true; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater)ListViewTest.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ; final View view = inflater.inflate(R.layout.layout_listview_item_select_multi_1, null) ; TextView tv = (TextView)view.findViewById(R.id.textView1) ; tv.setText(data.get(position)) ; CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox1) ; cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckBox cb = (CheckBox)v ; if(cb.isChecked()){ view.setBackgroundResource(R.drawable.list_selector2) ; }else{ view.setBackgroundResource(R.drawable.list_selector3) ; } } }) ; return view; } @Override public int getItemViewType(int position) { return 0; } @Override public int getViewTypeCount() { return 1; } @Override public boolean isEmpty() { return false; } @Override public boolean areAllItemsEnabled() { return true; } @Override public boolean isEnabled(int position) { return true; } } ;当然重写Adapter,不必实现 ListAdapter. 继承BaseAdaper 或者她的子类就行。