Android編程之listView中checkbox用法實(shí)例分析
本文實(shí)例講述了Android編程之listView中checkbox用法。分享給大家供大家參考,具體如下:
我們經(jīng)常會(huì)用到在listView中使用checkbox的情況。直接不回應(yīng)用后會(huì)發(fā)現(xiàn),ListView中的OnItemClickListener事件會(huì)和checkbox中的選擇事件發(fā)生沖突,這個(gè)怎么處理呢。直接上代碼。
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" <!--這個(gè)是必須加上的,不然會(huì)出現(xiàn)沖突-->
android:clickable="false" <!--這個(gè)是必須加上的,不然會(huì)出現(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這個(gè)map是記錄每個(gè)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)用頁(yè)面:
list_popwin_note_tag.setAdapter(new Add_note_tag_list_BaseAdapter(this, list_noteTag_date)); list_popwin_note_tag.setOnItemClickListener(new noteTagListItemOnClickListener());
/**
* 點(diǎn)擊列表項(xiàng)事件
* @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();
//在每次獲取點(diǎn)擊的item時(shí)將對(duì)于的checkbox狀態(tài)改變,同時(shí)修改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);
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android ListView ImageView實(shí)現(xiàn)單選按鈕實(shí)例
- android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
- Android中ListView綁定CheckBox實(shí)現(xiàn)全選增加和刪除功能(DEMO)
- Android MVP模式ListView中嵌入checkBox的使用方法
- Android中ListView結(jié)合CheckBox實(shí)現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
- Android checkbox的listView具體操作方法
- Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
- Android在listview添加checkbox實(shí)現(xiàn)原理與代碼
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
相關(guān)文章
Android編程之創(chuàng)建自己的內(nèi)容提供器實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android Toast使用的簡(jiǎn)單小結(jié)(推薦)
這篇文章主要介紹了Android Toast使用的簡(jiǎn)單小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實(shí)現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04
Android studio設(shè)置指定的簽名文件教程
這篇文章主要介紹了Android studio設(shè)置指定的簽名文件教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題
做開(kāi)發(fā)除了實(shí)現(xiàn)功能,還要注重優(yōu)化,性能優(yōu)化包括的東西還是非常多的,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-07-07

