listview中item 有checkbox多選防止滑動 listview頁面 出現checkbox錯位問題

來源:互聯網
上載者:User

標籤:

checkbox點擊切換背景

<CheckBox
android:id="@+id/checkbox"
android:layout_width="40dp"
android:layout_height="40dp"
android:button="@null"
android:focusable="false"
android:background="@drawable/playimage" />

 
   

drawable檔案夾下切換背景

/Day07_music/res/drawable/playimage.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@drawable/pause"></item>
<item android:state_checked="true" android:drawable="@drawable/pause"></item>
<item android:state_pressed="true" android:drawable="@drawable/pause"></item>
<item android:drawable="@drawable/forward"></item>

</selector>

 
   

適配器下面找到checkbox

在list集合裡設定 一個 boolean 值 記錄選中狀態

在適配器下對checkbox進行監聽

解釋:通過setOnCheckedChangeListener監聽選項組 然後通過setOnClickListener判斷目前狀態進行操作 避免因為滑動listview介面導致checkbox值改變,也就是說checkbox值是使用者點擊而改變避免滑動listview介面自動改變

holder.check.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//得到目前狀態
boolean check = list.get(position).isCheck();
if (check) {//判斷狀態

// 選中時要執行的操作

} else {

// 沒選中時要執行的操作

}
//重新整理適配器
notifyDataSetChanged();
}
});
holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

//選項組
list.get(position).setCheck(isChecked);

}

});
//當前checkbox狀態
holder.check.setChecked(list.get(position).isCheck());

 
   

listview中item 有checkbox多選防止滑動 listview頁面 出現checkbox錯位問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.