在Android某些開發需求當中,有時候須要在listveiw中加入checkbox實現單選,多選操做。表面上看上去只是改變checkbox那麼簡單,然而實際開發中,實現起來並非那麼駕輕就熟。尤爲當listview比較多(好比屏幕最多隻能顯示10個item,但總共有12個item,也就是說listview的item數大於屏幕可以顯示的item數)滑動屏幕的時候,因爲適配器中getview()會重複使用被移除屏幕的item,因此會形成checkbox選擇狀態不正常的現象。本身在開發中碰到這樣的問題非常苦惱,查了下資料,發現網上不多沒有針對這類批量操做並無一個完整的例子。搜了不少篇帖子才完美的實現這一經常使用的操做。因此在這裏把這個Demo貼出來,供你們參考,但願能對你們有所幫助。html
主界面的佈局main.xml 這個就很少說什麼java
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="100dip" android:text="原創:Simtice QQ:512375320" android:layout_marginLeft="10dip" /> </LinearLayout><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="50dip" android:textColor="#FCFCFC" android:textSize="11pt" android:gravity="center_vertical" android:layout_marginLeft="10dip" /> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="381dip" android:cacheColorHint ="#00000000" ></ListView> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="53dip" android:orientation="horizontal" > <Button android:id="@+id/selectall" android:layout_width="80dip" android:layout_height="50dip" android:layout_marginLeft="20dip" android:text="全選" android:gravity="center" /> <Button android:id="@+id/inverseselect" android:layout_width="80dip" android:layout_height="50dip" android:layout_marginLeft="118dip" android:text="反選" android:gravity="center" /> <Button android:id="@+id/cancel" android:layout_width="80dip" android:layout_height="50dip" android:layout_marginLeft="213dip" android:text="取消已選" android:gravity="center" /> </RelativeLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="100dip" android:text="原創:Simtice QQ:512375320" android:layout_marginLeft="10dip" /> </LinearLayout> </LinearLayout>
ListView每一個item的佈局,listviewitem.xml:android
這裏須要注意的是,因爲checkbox的點擊事件優先級比listview的高,因此要添加android:focusable="false"屬性,使得checkbox初始的時候沒有獲取焦點。ide
另外這裏是點擊ListView的item控制checkbox的狀態改變,也就是讓item接收clik事件,因此須要加上android:focusableInTouchMode="false"這一屬性。佈局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="55dip" android:orientation="horizontal" android:layout_marginTop="20dip" > <TextView android:id="@+id/item_tv" android:layout_width="267dip" android:layout_height="40dip" android:textSize="10pt" android:gravity="center_vertical" android:layout_marginLeft="10dip" /> <CheckBox android:id="@+id/item_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:clickable="false" android:layout_toRightOf="@id/item_tv" android:layout_alignParentTop="true" android:layout_marginRight="5dip" /> </RelativeLayout
ViewHolder類this
package simtice.test.listview.viewholder; import android.widget.CheckBox; import android.widget.TextView; public class ViewHolder { public TextView tv = null; public CheckBox cb = null; }
爲listview自定義適配器,該類爲主Activity類MainActivity.java的內部類spa
class MyAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Item getItem(int arg0) { return list.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View view, ViewGroup arg2) { System.out.println("getView " + position + " " + view); ViewHolder holder; if (view == null || (holder = (ViewHolder) view.getTag()) == null) { view = View.inflate(MainActivity.this, R.layout.listviewitem, null); holder = new ViewHolder(); holder.tv = (TextView) view.findViewById(R.id.item_tv); holder.cb = (CheckBox) view.findViewById(R.id.item_cb); view.setTag(holder); } Item item = getItem(position); holder.tv.setText(item.name); holder.cb.setChecked(item.status); return view; }
最後,最重要的就是MainActivity.java中一些事件響應的處理3d
public class MainActivity extends Activity implements OnClickListener { TextView tv = null; ListView lv = null; String name[] = { "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", "G13", "G14" }; private List<Item> list; private List<String> data; private MyAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) this.findViewById(R.id.tv); lv = (ListView) this.findViewById(R.id.lv); this.findViewById(R.id.selectall).setOnClickListener(this); this.findViewById(R.id.inverseselect).setOnClickListener(this); this.findViewById(R.id.cancel).setOnClickListener(this); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Item item = list.get(arg2); item.status = !item.status;// 取反 initAdapter(); } }); init(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.selectall: int size1 = list.size(); for (int i = 0; i < size1; i++) { list.get(i).status = true; } break; case R.id.inverseselect: int size2 = list.size(); for (int i = 0; i < size2; i++) { Item item = list.get(i); item.status = !item.status;// 取反 } break; case R.id.cancel: int size3 = list.size(); for (int i = 0; i < size3; i++) { list.get(i).status = false; } break; } initAdapter(); } // 數據初始化 private void init() { if (list == null) list = new ArrayList<Item>(); else list.clear(); if (data == null) data = new ArrayList<String>(); for (String s : name) { list.add(new Item(s, false)); } initAdapter(); } // 刷新適配器 public void initAdapter() { if (adapter == null) { adapter = new MyAdapter(); lv.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } int size = list.size(); data.clear(); for (int i = 0; i < size; i++) { if (list.get(i).status) /*這裏能夠處理checkbox爲true時的事件*/ data.add(name[i]); else /*爲false時的事件*/ data.remove(name[i]); } tv.setText("已選中 " + data.size() + " 項"); } // 爲listview自定義適配器內部類 class MyAdapter extends BaseAdapter { ... } class Item { public String name; public boolean status = false; public Item(String name, boolean b) { this.name = name; this.status = b; } }
我選擇了G二、G三、G11三項,如今屏幕滑動到底部,看以看到狀態保存的很好,TextView顯示已選中3項。全選、反選、取消已選功能正常,多選操做完美解決!code
源碼:http://www.eoeandroid.com/thread-152037-1-1.htmlxml