Android開發(fā)中使用ListView 與 CheckBox實(shí)現(xiàn)一個(gè)多選框功能?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、興和網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、興和網(wǎng)絡(luò)營銷、興和企業(yè)策劃、興和品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供興和建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:vcdvsql.cn
效果:
1 單選
public class SingleActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> groups; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single); listView = (ListView) this.findViewById(R.id.lvGroup); groups = new ArrayList<String>(); groups.add("11"); groups.add("22"); groups.add("33"); groups.add("44"); groups.add("55"); groups.add("66"); groups.add("77"); AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 取得ViewHolder對象,這樣就省去了通過層層的findViewById去實(shí)例化我們需要的cb實(shí)例的步驟 SingleAdapter.ViewHolder viewHolder = (SingleAdapter.ViewHolder) view.getTag(); viewHolder.cb.toggle();// 把CheckBox的選中狀態(tài)改為當(dāng)前狀態(tài)的反,gridview確保是單一選中 } }; SingleAdapter adapter = new SingleAdapter(this, groups); listView.setAdapter(adapter); listView.setOnItemClickListener(listItemClickListener); } }
Adapter:
public class SingleAdapter extends BaseAdapter { private Activity activity;//上下文 private ArrayList<String> list; private LayoutInflater inflater = null;//導(dǎo)入布局 private int temp = -1; public SingleAdapter(Activity context, ArrayList<String> list) { this.activity = context; this.list = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } //listview每顯示一行數(shù)據(jù),該函數(shù)就執(zhí)行一次 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) {//當(dāng)?shù)谝淮渭虞dListView控件時(shí) convertView為空 convertView = inflater.inflate(R.layout.group_item_view, null);//所以當(dāng)ListView控件沒有滑動時(shí)都會執(zhí)行這條語句 holder = new ViewHolder(); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); convertView.setTag(holder);//為view設(shè)置標(biāo)簽 } else {//取出holder holder = (ViewHolder) convertView.getTag(); } //設(shè)置list的textview顯示 holder.tv.setTextColor(Color.WHITE); holder.tv.setText(list.get(position)); // 根據(jù)isSelected來設(shè)置checkbox的選中狀況 holder.cb.setId(position);//對checkbox的id進(jìn)行重新設(shè)置為當(dāng)前的position holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { if (isChecked) {//實(shí)現(xiàn)checkbox的單選功能,同樣適用于radiobutton if (temp != -1) { //找到上次點(diǎn)擊的checkbox,并把它設(shè)置為false,對重新選擇時(shí)可以將以前的關(guān)掉 CheckBox tempCheckBox = (CheckBox) activity.findViewById(temp); if (tempCheckBox != null) tempCheckBox.setChecked(false); } temp = compoundButton.getId();//保存當(dāng)前選中的checkbox的id值 } } }); if (position == temp)//比對position和當(dāng)前的temp是否一致 holder.cb.setChecked(true); else holder.cb.setChecked(false); return convertView; } public static class ViewHolder { TextView tv; CheckBox cb; } }
多選:
public class MulActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> groups; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mul); listView = (ListView) this.findViewById(R.id.list); groups = new ArrayList<>(); groups.add("11"); groups.add("22"); groups.add("33"); groups.add("44"); groups.add("55"); groups.add("66"); groups.add("77"); AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 取得ViewHolder對象,這樣就省去了通過層層的findViewById去實(shí)例化我們需要的cb實(shí)例的步驟 MulAdapter.ViewHolder viewHolder = (MulAdapter.ViewHolder) view.getTag(); viewHolder.cb.toggle();// 把CheckBox的選中狀態(tài)改為當(dāng)前狀態(tài)的反,gridview確保是單一選中 MulAdapter.getIsSelected().put(position, viewHolder.cb.isChecked());//將CheckBox的選中狀況記錄下來 } }; MulAdapter adapter = new MulAdapter(this, groups); listView.setAdapter(adapter); listView.setOnItemClickListener(listItemClickListener); } }
Adapter:
public class MulAdapter extends BaseAdapter { private Context context;//上下文 private ArrayList<String> list; //控制CheckBox選中情況 private static HashMap<Integer, Boolean> isSelected; private LayoutInflater inflater = null;//導(dǎo)入布局 public MulAdapter(Context context, ArrayList<String> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); isSelected = new HashMap<Integer, Boolean>(); initData(); } private void initData() {//初始化isSelected的數(shù)據(jù) for (int i = 0; i < list.size(); i++) { getIsSelected().put(i, false); } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } //listview每顯示一行數(shù)據(jù),該函數(shù)就執(zhí)行一次 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) {//當(dāng)?shù)谝淮渭虞dListView控件時(shí) convertView為空 convertView = inflater.inflate(R.layout.group_item_view, null);//所以當(dāng)ListView控件沒有滑動時(shí)都會執(zhí)行這條語句 holder = new ViewHolder(); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); convertView.setTag(holder);//為view設(shè)置標(biāo)簽 } else {//取出holder holder = (ViewHolder) convertView.getTag();//the Object stored in this view as a tag } //設(shè)置list的textview顯示 holder.tv.setTextColor(Color.WHITE); holder.tv.setText(list.get(position)); // 根據(jù)isSelected來設(shè)置checkbox的選中狀況 holder.cb.setChecked(getIsSelected().get(position)); return convertView; } public static class ViewHolder { TextView tv; CheckBox cb; } public static HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer, Boolean> isSelected) { MulAdapter.isSelected = isSelected; } }
關(guān)于Android開發(fā)中使用ListView 與 CheckBox實(shí)現(xiàn)一個(gè)多選框功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:Android開發(fā)中使用ListView與CheckBox實(shí)現(xiàn)一個(gè)多選框功能
轉(zhuǎn)載來于:http://vcdvsql.cn/article38/gghgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、微信公眾號、移動網(wǎng)站建設(shè)、軟件開發(fā)、關(guān)鍵詞優(yōu)化、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)