Android在listview添加checkbox實(shí)現(xiàn)原理與代碼

主界面CheckBoxinListViewActivity.java代碼如下:
public class CheckBoxinListViewActivity extends Activity {
/** Called when the activity is first created. */
private MyAdapter adapter;
private ListView listview;
private Button checkAll;
private Button noCheckAll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView)findViewById(R.id.listview);
checkAll = (Button)findViewById(R.id.button1);
noCheckAll = (Button)findViewById(R.id.button2);
adapter = new MyAdapter();
listview.setAdapter(adapter);
checkAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.checkAll();
}
});
noCheckAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.noCheckAll();
}
});
}
private class MyAdapter extends BaseAdapter{
private ArrayList<Message> list= new ArrayList<Message>();
public MyAdapter(){
for(int i = 1 ;i<=100 ; i++){
list.add(new Message("item_"+i));
}
}
public void checkAll(){
for(Message msg:list){
msg.isCheck = true;
}
notifyDataSetChanged();
}
public void noCheckAll(){
for(Message msg:list){
msg.isCheck = false;
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(CheckBoxinListViewActivity.this);
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
final Message msg = list.get(position);
viewHolder.checkBox.setText(msg.str);
viewHolder.checkBox.setChecked(msg.isCheck);
//注意這里設(shè)置的不是onCheckedChangListener,還是值得思考一下的
viewHolder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(msg.isCheck){
msg.isCheck = false;
}else{
msg.isCheck = true;
}
}
});
return convertView;
}
}
private class ViewHolder{
CheckBox checkBox;
}
}
適配器所適配的消息Message.java如下:
public class Message {
public boolean isCheck;
public String str;
public Message(String str){
this.str = str;
}
}
main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="全選" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="撤消" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<ListView android:id="@+id/listview" android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</LinearLayout>
listview_item.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<CheckBox
android:text="CheckBox"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
囧神的世界你不懂,蟲哥的生活你沒(méi)有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
- Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
- android RadioButton和CheckBox組件的使用方法
- 詳解Android Checkbox的使用方法
- android開發(fā)教程之自定義控件checkbox的樣式示例
- Android控件系列之CheckBox使用介紹
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
- Android中ListView結(jié)合CheckBox實(shí)現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
- Android CheckBox 的使用案例分析
- Android中自定義Checkbox組件實(shí)例
- Android中CheckBox復(fù)選框控件使用方法詳解
相關(guān)文章
ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析
這篇文章主要為大家介紹了ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼
這篇文章主要介紹了100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android實(shí)現(xiàn)完整游戲循環(huán)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)完整游戲循環(huán)的方法,以實(shí)例代碼形式較為詳細(xì)的分析了Android游戲循環(huán)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10為Android Studio編寫自定義Gradle插件的教程
這篇文章主要介紹了為Android Studio編寫自定義Gradle插件的教程,Android Studio現(xiàn)在基本上已經(jīng)成為了安卓開發(fā)的標(biāo)配IDE,友可以參考下2016-02-02Android編程實(shí)現(xiàn)的身份證、車牌號(hào)正則驗(yàn)證工具類實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的身份證、車牌號(hào)正則驗(yàn)證工具類,結(jié)合實(shí)例形式分析了java針對(duì)身份證、車牌號(hào)數(shù)字字符串正則驗(yàn)證操作技巧,需要的朋友可以參考下2017-12-12Android通過(guò)ksoap2傳遞復(fù)雜數(shù)據(jù)類型及CXF發(fā)布的webservice詳細(xì)介紹
這篇文章主要介紹了 Android通過(guò)ksoap2傳遞復(fù)雜數(shù)據(jù)類型詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android Adapter里面嵌套ListView實(shí)例詳解
這篇文章主要介紹了Android Adapter里面嵌套ListView實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼并說(shuō)明如何實(shí)現(xiàn)該功能,需要的朋友可以參考下2017-07-07