Android編程之listView中checkbox用法實(shí)例分析
本文實(shí)例講述了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());
/**
* 點(diǎn)擊列表項事件
* @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時將對于的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實(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 IPC機(jī)制Messenger實(shí)例詳解
這篇文章主要介紹了 Android IPC機(jī)制Messenger實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android中手機(jī)震動的設(shè)置(Vibrator)的步驟簡要說明
Android中手機(jī)震動的設(shè)置(Vibrator)的步驟,很詳細(xì),感興趣的朋友可以了解下哦2013-01-01
利用Kotlin的方式如何處理網(wǎng)絡(luò)異常詳解
這篇文章主要 給大家介紹了關(guān)于利用Kotlin的方式如何處理網(wǎng)絡(luò)異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android實(shí)現(xiàn)圖片加載進(jìn)度提示
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片加載進(jìn)度提示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

