轉自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=152037
在Android某些開發需求當中,有時候需要在listveiw中加入checkbox實現單選,多選操作。表面上看上去只是改變checkbox那麼簡單,然而實際開發中,實現起來並不是那麼得心應手。尤其當listview比較多(比如螢幕最多隻能顯示10個item,但總共有12個item,也就是說listview的item數大於螢幕能夠顯示的item數)滑動螢幕的時候,由於適配器中getview()會重複使用被移除螢幕的item,所以會造成checkbox選擇狀態不正常的現象。自己在開發中碰到這樣的問題很是苦惱,查了下資料,發現網上很少沒有針對這類大量操作並沒有一個完整的例子。搜了很多篇文章才完美的實現這一常用的操作。所以在這裡把這個Demo貼出來,供大家參考,希望能對大家有所協助。
主介面的布局main.xml 這個就不多說什麼
<?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:
這裡需要注意的是,由於checkbox的點擊事件優先順序比listview的高,所以要添加android:focusable="false"屬性,使得checkbox初始的時候沒有擷取焦點。
另外這裡是點擊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類
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的內部類
public static class MyAdapter extends BaseAdapter { public static HashMap<Integer, Boolean> isSelected; private Context context = null; private LayoutInflater inflater = null; private List<HashMap<String, Object>> list = null; private String keyString[] = null; private String itemString = null; // 記錄每個item中textview的值 private int idValue[] = null;// id值 public MyAdapter(Context context, List<HashMap<String, Object>> list,int resource) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); init(); } // 初始化 設定所有checkbox都為未選擇 public void init() { isSelected = new HashMap<Integer, Boolean>(); for (int i = 0; i < list.size(); i++) { isSelected.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 0; } @Override public View getView(int position, View view, ViewGroup arg2) { ViewHolder holder = null; if (holder == null) { holder = new ViewHolder(); if (view == null) { view = inflater.inflate(R.layout.listviewitem, null); } holder.tv = (TextView) view.findViewById(R.id.item_tv); holder.cb = (CheckBox) view.findViewById(R.id.item_cb); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } if (map != null) { holder.tv.setText(list.get(position)); } holder.cb.setChecked(isSelected.get(position)); return view; } }
最後,最重要的就是MainActivity.java中一些事件響應的處理
public class MainActivity extends Activity { TextView tv = null; ListView lv = null; Button btn_selectAll = null; Button btn_inverseSelect = null; Button btn_calcel = null; String name[] = { "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", "G13", "G14" }; ArrayList<String> listStr = null; private List<HashMap<String, Object>> list = null; 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); btn_selectAll = (Button) this.findViewById(R.id.selectall); btn_inverseSelect = (Button) this.findViewById(R.id.inverseselect); btn_calcel = (Button) this.findViewById(R.id.cancel); showCheckBoxListView(); //全選 btn_selectAll.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { listStr = new ArrayList<String>(); for(int i=0;i<list.size();i++){ MyAdapter.isSelected.put(i,true); listStr.add(name[i]); } adapter.notifyDataSetChanged();//注意這一句必須加上,否則checkbox無法正常更新狀態 tv.setText("已選中"+listStr.size()+"項"); } }); //反選 btn_inverseSelect.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { for(int i=0;i<list.size();i++){ if(MyAdapter.isSelected.get(i)==false){ MyAdapter.isSelected.put(i, true); listStr.add(name[i]); } else{ MyAdapter.isSelected.put(i, false); listStr.remove(name[i]); } } adapter.notifyDataSetChanged(); tv.setText("已選中"+listStr.size()+"項"); } }); //取消已選 btn_calcel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { for(int i=0;i<list.size();i++){ if(MyAdapter.isSelected.get(i)==true){ MyAdapter.isSelected.put(i, false); listStr.remove(name[i]); } } adapter.notifyDataSetChanged(); tv.setText("已選中"+listStr.size()+"項"); } }); } // 顯示帶有checkbox的listview public void showCheckBoxListView() { list = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < name.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("item_tv", name[i]); map.put("item_cb", false); list.add(map); adapter = new MyAdapter(this, list, R.layout.listviewitem, new String[] { "item_tv", "item_cb" }, new int[] { R.id.item_tv, R.id.item_cb }); lv.setAdapter(adapter); listStr = new ArrayList<String>(); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { ViewHolder holder = (ViewHolder) view.getTag(); // 在每次擷取點擊的item時改變checkbox的狀態 holder.cb.toggle(); // 同時修改map的值儲存狀態 MyAdapter.isSelected.put(position, holder.cb.isChecked()); if (holder.cb.isChecked() == true) { listStr.add(name[position]); } else { listStr.remove(name[position]); } tv.setText("已選中"+listStr.size()+"項"); } }); } } //為listview自訂配接器內部類 public static class MyAdapter extends BaseAdapter { ... }}
好了,來看運行結果
我選擇了G2、G3、G11三項,現在螢幕滑動到底部,可以看到狀態儲存的很好,TextView顯示已選中3項。全選、反選、取消已選功能正常,多選操作完美解決!
ListViewForCheckbox.rar(65.9
KB, 下載次數: 246)
2011-12-21 20:33 上傳
點擊檔案名稱下載附件
下載積分: e幣 -1 元
經過幾位朋友的指教,感覺功能雖然實現了,但是代碼在設計編寫方面還是有很大問題的。感謝38樓朋友修改的代碼,我覺得寫的非常漂亮,自己又小小的修改了一下,更加完美了。多謝各位朋友的指點。
listviewcheckbox1.rar(93.43
KB, 下載次數: 502)
另:http://blog.sina.com.cn/s/blog_79014b2301017ejd.html