listview和checkbox聯(lián)動單選的處理

最近做了這個需求選擇顏色在這里記錄一下下面是布局里面基本一個listview


? ? xmlns:app="http://schemas.android.com/apk/res-auto"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:background="#EEEEEE"

? ? android:orientation="vertical"

? ? tools:context="com.atgerunkeji.example.carschool.controller.activity.RichengyanseActivity">

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_marginLeft="16dp"

? ? ? ? android:layout_marginTop="17dp"

? ? ? ? android:singleLine="true"

? ? ? ? android:text="選擇顏色"

? ? ? ? android:textColor="#ff999999"

? ? ? ? android:textSize="13sp" />

? ? ? ? android:id="@+id/iv_yanse"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="200dp"

? ? ? ? android:layout_marginTop="13dp"

? ? ? ? android:background="@color/white">

? ? ? ? android:id="@+id/btn_queren"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="40dp"

? ? ? ? android:layout_marginLeft="16dp"

? ? ? ? android:layout_marginRight="16dp"

? ? ? ? android:layout_marginTop="32dp"

? ? ? ? android:background="@drawable/iv_shapeblue"

? ? ? ? android:text="確認(rèn)"

? ? ? ? android:textColor="@color/white"

? ? ? ? android:textSize="17sp" />

創(chuàng)建數(shù)據(jù)

datas =new ArrayList<>();

datas.add("工作");

datas.add("學(xué)習(xí)");

datas.add("運動");

datas.add("重要日");

設(shè)置適配器

myRichengyanseAdapter =new MyRichengyanseAdapter(context,datas);

ivYanse.setAdapter(myRichengyanseAdapter);

設(shè)置item點擊事件

ivYanse.setOnItemClickListener(this);

這是adapter中的內(nèi)容

class MyRichengyanseAdapterextends BaseAdapter {

private Contextcontext;

private ArrayListdata;

private int tempPosition = -1;//記錄已經(jīng)點擊的CheckBox的位置

? ? private Integer[]images = {R.drawable.personal_icon_work, R.drawable.personal_icon_study, R.drawable.personal_icon_sport, R.drawable.personal_icon_import};

public MyRichengyanseAdapter(Context context, ArrayList data) {

this.context = context;

this.data = data;

}

@Override

? ? public int getCount() {

return data.size();

}

@Override

? ? public Object getItem(int position) {

return data.get(position);

}

@Override

? ? public long getItemId(int position) {

return position;

}

@Override

? ? public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder =null;

if (convertView ==null) {

convertView = View.inflate(context, R.layout.item_richengyanse,null);

holder =new ViewHolder();

holder.chebox = convertView.findViewById(R.id.chebox);

holder.iv_yanse = convertView.findViewById(R.id.iv_yanse);

holder.tv_gongzuo = convertView.findViewById(R.id.tv_gongzuo);

convertView.setTag(holder);

}else {

holder = (ViewHolder) convertView.getTag();

}

holder.iv_yanse.setImageResource(images[position]);

holder.tv_gongzuo.setText(data.get(position));

holder.chebox.setId(position);//設(shè)置當(dāng)前position為CheckBox的id

? ? ? ? holder.chebox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

? ? ? ? ? ? public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if (isChecked) {

if (tempPosition != -1) {

//根據(jù)id找到上次點擊的CheckBox,將它設(shè)置為false.

? ? ? ? ? ? ? ? ? ? ? ? CheckBox tempCheckBox = (CheckBox) findViewById(tempPosition);

if (tempCheckBox !=null) {

tempCheckBox.setChecked(false);

}

}

//保存當(dāng)前選中CheckBox的id值

? ? ? ? ? ? ? ? ? ? tempPosition = buttonView.getId();

}else {//當(dāng)CheckBox被選中,又被取消時,將tempPosition重新初始化.

? ? ? ? ? ? ? ? ? ? tempPosition = -1;

}

}

});

if (position ==tempPosition)//比較位置是否一樣,一樣就設(shè)置為選中,否則就設(shè)置為未選中.

? ? ? ? {

holder.chebox.setChecked(true);

}else {

holder.chebox.setChecked(false);

}

return convertView;

}

//返回當(dāng)前CheckBox選中的位置,便于獲取值.

? ? public int getSelectPosition() {

return tempPosition;

}

class ViewHolder {

ImageViewiv_yanse;

TextViewtv_gongzuo;

CheckBoxchebox;

}

}

item布局


? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent">

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="48dp">

? ? ? ? ? ? android:id="@+id/iv_yanse"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_centerVertical="true"

? ? ? ? ? ? android:layout_marginLeft="20dp" />

? ? ? ? ? ? android:id="@+id/tv_gongzuo"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_centerVertical="true"

? ? ? ? ? ? android:layout_marginLeft="48dp"

? ? ? ? ? ? android:text="工作"

? ? ? ? ? ? android:textColor="#ff333333"

? ? ? ? ? ? android:textSize="15sp" />

? ? ? ? ? ? android:id="@+id/chebox"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_alignParentRight="true"

? ? ? ? ? ? android:layout_centerVertical="true"

? ? ? ? ? ? android:layout_marginRight="16dp" />


//實現(xiàn)聯(lián)動效果

@Override

public void onItemClick(AdapterView parent, View view,int position,long id) {

MyRichengyanseAdapter.ViewHolder holder = (MyRichengyanseAdapter.ViewHolder) view.getTag();

holder.chebox.toggle();

}


@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.tvleft:

finish();

break;

case R.id.btn_queren:

//確認(rèn)

? ? ? ? ? ? int selectPosition =myRichengyanseAdapter.getSelectPosition();

if(selectPosition==-1) {

ToastUtils.showToast(context,"請選擇日程顏色");

return;

}

String s =datas.get(selectPosition);

if (TextUtils.isEmpty(s)) {

ToastUtils.showToast(context,"請選擇日程顏色");

return;

}

//Activity返回時傳遞數(shù)據(jù)延曙,也是通過意圖對象

? ? ? ? ? ? Intent data =new Intent();

//把要傳遞的數(shù)據(jù)封裝至意圖對象中

? ? ? ? ? ? data.putExtra("richengyanse", s);

//當(dāng)前Activity銷毀時遭笋,data這個意圖就會傳遞給啟動當(dāng)前Activity的那個Activity

? ? ? ? ? ? setResult(2000, data);

finish();

break;

default:

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末律适,一起剝皮案震驚了整個濱河市反症,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晋柱,死亡現(xiàn)場離奇詭異,居然都是意外死亡诵叁,警方通過查閱死者的電腦和手機(jī)雁竞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拧额,“玉大人碑诉,你說我怎么就攤上這事∈迫” “怎么了联贩?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長捎拯。 經(jīng)常有香客問我泪幌,道長盲厌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任祸泪,我火速辦了婚禮吗浩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘没隘。我一直安慰自己懂扼,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布右蒲。 她就那樣靜靜地躺著阀湿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瑰妄。 梳的紋絲不亂的頭發(fā)上陷嘴,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機(jī)與錄音间坐,去河邊找鬼灾挨。 笑死,一個胖子當(dāng)著我的面吹牛竹宋,可吹牛的內(nèi)容都是我干的劳澄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蜈七,長吁一口氣:“原來是場噩夢啊……” “哼秒拔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起飒硅,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤溯警,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后狡相,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡食磕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年尽棕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片彬伦。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡滔悉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出单绑,到底是詐尸還是另有隱情回官,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布搂橙,位于F島的核電站歉提,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苔巨,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一版扩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧侄泽,春花似錦礁芦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至闺魏,卻和暖如春未状,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舷胜。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工娩践, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人烹骨。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓翻伺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親沮焕。 傳聞我的和親對象是個殘疾皇子吨岭,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容