ListActivity中CheckBox的勾选事件处理

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

ListActivity中CheckBox的勾选事件处理

当ListActivity中包含Button或CheckBox这些能得到焦点控件时,要让List相应OnListItemClick事件,只要让Button或CheckBox的属性focusable=“false"

my_list_item.xml:(注意layout文件中checkBox的属性focusable的设置)

<?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="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"        
        android:text="text view"        
        android:focusable="false" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>
关于CheckBox的事件处理,网上看到的一些方法比较复杂,这里提供一种比较方便的方法:派生一个SimpleAdaper的子类,只要重写它的getView()方法,在方法体里为checkBox添加OnCheckChangedListener即可,并将CheckBox.tag字段这为这个checkBox所在的行数,方便知道是那一行的checkBox被勾选
public class MyListActivity extends ListActivity implements OnCheckedChangeListener, OnItemLongClickListener {

	private ArrayList<HashMap<String, Object>> listItems;
	private SimpleAdapter listItemAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		listItems = new ArrayList<HashMap<String,Object>>();
		listItemAdapter = new MySimpleAdapter(this, listItems,
				R.layout.my_list_item,
				new String[]{"text","check"},
				new int[]{R.id.textView1, R.id.check});
		
		setListAdapter(listItemAdapter);
		
		for(int i=0;i<3;i++) {
			HashMap<String, Object>map = new HashMap<String, Object>();
			map.put("text", "第"+i+"行");
			map.put("check", true);
			listItems.add(map);
		}
		
		// 在activity的底部添加一个不属于list里面的button
	    Button addButton=new Button(this);
	    addButton.setText("button");
	    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
	    FrameLayout.LayoutParams.WRAP_CONTENT);
	    lp.gravity=Gravity.BOTTOM;
	    addContentView(addButton,lp);
	    
	    // 长按:
	    getListView().setOnItemLongClickListener(this);
	    
	    // 刷新:
	    setListAdapter(listItemAdapter);
	}
	
	// 写一个能监听checkBox勾选事件的SimpleAdapter,如果不是checkBox而是button.onClick事件也是类似的
	private class MySimpleAdapter extends SimpleAdapter {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view =  super.getView(position, convertView, parent);
			if(view!=null) {
				CheckBox cb = (CheckBox) view.findViewById(R.id.check);
				// 小技巧:checkBox 的  tag 为它所在的行,在onCheckedChanged方法里面用到
				cb.setTag(position);
				cb.setOnCheckedChangeListener(MyListActivity.this);
			}
			return view;
		}
		
		public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
		
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		int row = (Integer) buttonView.getTag();
		Toast.makeText(this, "第"+row+"行的checkBox被点击", Toast.LENGTH_LONG).show();		
	}

	public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
		Toast.makeText(this, "第"+position+"行被长按", Toast.LENGTH_LONG).show();
		return true;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Toast.makeText(this, "第"+position+"行被点击", Toast.LENGTH_LONG).show();
	}
}

你可能感兴趣的:(checkbox,ListActivity,LongClick)