Android編程之listView中checkbox用法實例分析
本文實例講述了Android編程之listView中checkbox用法。分享給大家供大家參考,具體如下:
我們經(jīng)常會用到在listView中使用checkbox的情況。直接不回應(yīng)用后會發(fā)現(xiàn),ListView中的OnItemClickListener事件會和checkbox中的選擇事件發(fā)生沖突,這個怎么處理呢。直接上代碼。
list_item.xml代碼:
<?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" android:background="@color/color_while"> <TextView android:id="@+id/txt_add_note_tag_list_name" android:layout_height="50dp" android:layout_width="fill_parent" android:gravity="center_vertical" android:textColor="@color/color_black" android:layout_marginLeft="8dp" /> <CheckBox android:id="@+id/chk_add_note_tag_list_chk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_alignParentRight="true" android:layout_marginRight="8dp" android:focusable="false" <!--這個是必須加上的,不然會出現(xiàn)沖突--> android:clickable="false" <!--這個是必須加上的,不然會出現(xiàn)沖突--> /> </RelativeLayout>
BaseAdapter.java代碼:
package cg.guangda.androidnote; import java.util.HashMap; import java.util.List; import java.util.Map; import cg.guangda.androidnote.Model.noteTag; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; public class Add_note_tag_list_BaseAdapter extends BaseAdapter { private LayoutInflater inflater; private List<noteTag> list_notetag_date; //定義多選框是否被選中 public static Map<Integer, Boolean> isSelected; public Add_note_tag_list_BaseAdapter(Context context,List<noteTag> list_notetag_date) { this.inflater = LayoutInflater.from(context); this.list_notetag_date = list_notetag_date; //這兒定義isSelected這個map是記錄每個listitem的狀態(tài),初始狀態(tài)全部為false。 isSelected = new HashMap<Integer, Boolean>(); for (int i = 0; i < list_notetag_date.size(); i++) { isSelected.put(i, false); } } @Override public int getCount() { // TODO Auto-generated method stub return list_notetag_date.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list_notetag_date.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { add_note_noteTag notetag = null; if(convertView==null) { convertView = inflater.inflate(R.layout.add_note_tag_list_item, null); notetag = new add_note_noteTag(); notetag.txt_add_note_tag_list_name = (TextView)convertView.findViewById(R.id.txt_add_note_tag_list_name); notetag.chk_add_note_tag_list_chk = (CheckBox)convertView.findViewById(R.id.chk_add_note_tag_list_chk); convertView.setTag(notetag); } else { notetag = (add_note_noteTag)convertView.getTag(); } notetag.txt_add_note_tag_list_name.setText(list_notetag_date.get(position).get_tagName()); notetag.chk_add_note_tag_list_chk.setChecked(isSelected.get(position)); return convertView; } public class add_note_noteTag { TextView txt_add_note_tag_list_name; CheckBox chk_add_note_tag_list_chk; } }
應(yīng)用頁面:
list_popwin_note_tag.setAdapter(new Add_note_tag_list_BaseAdapter(this, list_noteTag_date)); list_popwin_note_tag.setOnItemClickListener(new noteTagListItemOnClickListener());
/** * 點擊列表項事件 * @author cg * */ class noteTagListItemOnClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { // TODO Auto-generated method stub add_note_noteTag vHollder = (add_note_noteTag) view.getTag(); //在每次獲取點擊的item時將對于的checkbox狀態(tài)改變,同時修改map的值。 vHollder.chk_add_note_tag_list_chk.setChecked(vHollder.chk_add_note_tag_list_chk.isChecked()==true ? false : true); boolean check = vHollder.chk_add_note_tag_list_chk.isChecked(); Add_note_tag_list_BaseAdapter.isSelected.put(position, check); Log.v("noteTagListItemOnClickListener", list_noteTag_date.get(position).get_tagName() + check); } }
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android ListView ImageView實現(xiàn)單選按鈕實例
- android基于ListView和CheckBox實現(xiàn)多選和全選記錄的功能
- Android中ListView綁定CheckBox實現(xiàn)全選增加和刪除功能(DEMO)
- Android MVP模式ListView中嵌入checkBox的使用方法
- Android中ListView結(jié)合CheckBox實現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
- Android checkbox的listView具體操作方法
- Android checkbox的listView(多選,全選,反選)具體實現(xiàn)方法
- Android在listview添加checkbox實現(xiàn)原理與代碼
- Android中ListView + CheckBox實現(xiàn)單選、多選效果
相關(guān)文章
Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法,結(jié)合具體實例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04Android studio設(shè)置指定的簽名文件教程
這篇文章主要介紹了Android studio設(shè)置指定的簽名文件教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03