这个小例子包含:ListActivity中CheckBox的勾选事件处理,List中Item的单击和长按事件处理,以及在list下方显示一个不随List滚动的Button

当ListActivity中包含Button或CheckBox这些能得到焦点控件时,要让List相应OnListItemClick事件,只要让Button或CheckBox的属性focusable=“false"
my_list_item.xml:(注意layout文件中checkBox的属性focusable的设置)
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:id="@+id/textView1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_alignParentLeft="true" 11 android:layout_alignParentTop="true" 12 android:text="text view" 13 android:focusable="false" /> 14 15 <CheckBox 16 android:id="@+id/check" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_alignParentRight="true" 20 android:layout_alignParentTop="true" 21 android:focusable="false" 22 android:focusableInTouchMode="false" /> 23 24</RelativeLayout>
关于CheckBox的事件处理,网上看到的一些方法比较复杂,这里提供一种比较方便的方法:派生一个SimpleAdaper的子类,只要重写它的getView()方法,在方法体里为checkBox添加OnCheckChangedListener即可,并将CheckBox.tag字段这为这个checkBox所在的行数,方便知道是那一行的checkBox被勾选
1public class MyListActivity extends ListActivity implements OnCheckedChangeListener, OnItemLongClickListener { 2 3 private ArrayList<HashMap<String, Object>> listItems; 4 private SimpleAdapter listItemAdapter; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 10 listItems = new ArrayList<HashMap<String,Object>>(); 11 listItemAdapter = new MySimpleAdapter(this, listItems, 12 R.layout.my_list_item, 13 new String[]{"text","check"}, 14 new int[]{R.id.textView1, R.id.check}); 15 16 setListAdapter(listItemAdapter); 17 18 for(int i=0;i<3;i++) { 19 HashMap<String, Object>map = new HashMap<String, Object>(); 20 map.put("text", "第"+i+"行"); 21 map.put("check", true); 22 listItems.add(map); 23 } 24 25 // 在activity的底部添加一个不属于list里面的button 26 Button addButton=new Button(this); 27 addButton.setText("button"); 28 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 29 FrameLayout.LayoutParams.WRAP_CONTENT); 30 lp.gravity=Gravity.BOTTOM; 31 addContentView(addButton,lp); 32 33 // 长按: 34 getListView().setOnItemLongClickListener(this); 35 36 // 刷新: 37 setListAdapter(listItemAdapter); 38 } 39 40 // 写一个能监听checkBox勾选事件的SimpleAdapter,如果不是checkBox而是button.onClick事件也是类似的 41 private class MySimpleAdapter extends SimpleAdapter { 42 43 @Override 44 public View getView(int position, View convertView, ViewGroup parent) { 45 46 View view = super.getView(position, convertView, parent); 47 if(view!=null) { 48 CheckBox cb = (CheckBox) view.findViewById(R.id.check); 49 // 小技巧:checkBox 的 tag 为它所在的行,在onCheckedChanged方法里面用到 50 cb.setTag(position); 51 cb.setOnCheckedChangeListener(MyListActivity.this); 52 } 53 return view; 54 } 55 56 public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { 57 super(context, data, resource, from, to); 58 } 59 60 } 61 62 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 63 int row = (Integer) buttonView.getTag(); 64 Toast.makeText(this, "第"+row+"行的checkBox被点击", Toast.LENGTH_LONG).show(); 65 } 66 67 public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) { 68 Toast.makeText(this, "第"+position+"行被长按", Toast.LENGTH_LONG).show(); 69 return true; 70 } 71 72 @Override 73 protected void onListItemClick(ListView l, View v, int position, long id) { 74 Toast.makeText(this, "第"+position+"行被点击", Toast.LENGTH_LONG).show(); 75 } 76}