Android checkbox的listView(多选,全选,反选)具体实现方法
程序员文章站
2023-12-01 20:08:04
布局文件:[html] 复制代码 代码如下:
布局文件:
[html]
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<textview
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<linearlayout
android:id="@+id/line"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/tv"
android:orientation="horizontal" >
<button
android:id="@+id/bt_selectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="全选" />
<button
android:id="@+id/bt_cancleselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="反选" />
<button
android:id="@+id/bt_deselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="取消选择" />
</linearlayout>
<listview
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/line" />
</relativelayout>
listview 的item布局文件:
[html]
<?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="fill_parent"
android:orientation="horizontal" >
<textview
android:id="@+id/item_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<checkbox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableintouchmode="false"
android:gravity="center_vertical" />
</linearlayout>
activity:
[java]
public class ex_checkboxactivity extends activity {
private listview lv;
private myadapter madapter;
private arraylist<string> list;
private button bt_selectall;
private button bt_cancel;
private button bt_deselectall;
private int checknum; // 记录选中的条目数量
private textview tv_show;// 用于显示选中的条目数量
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
/* 实例化各个控件 */
lv = (listview) findviewbyid(r.id.lv);
bt_selectall = (button) findviewbyid(r.id.bt_selectall);
bt_cancel = (button) findviewbyid(r.id.bt_cancelselectall);
bt_deselectall = (button) findviewbyid(r.id.bt_deselectall);
tv_show = (textview) findviewbyid(r.id.tv);
list = new arraylist<string>();
// 为adapter准备数据
initdate();
// 实例化自定义的myadapter
madapter = new myadapter(list, this);
// 绑定adapter
lv.setadapter(madapter);
// 全选按钮的回调接口
bt_selectall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将myadapter中的map值全部设为true
for (int i = 0; i < list.size(); i++) {
myadapter.getisselected().put(i, true);
}
// 数量设为list的长度
checknum = list.size();
// 刷新listview和textview的显示
datachanged();
}
});
// 反选按钮的回调接口
bt_cancel.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将已选的设为未选,未选的设为已选
for (int i = 0; i < list.size(); i++) {
if (myadapter.getisselected().get(i)) {
myadapter.getisselected().put(i, false);
checknum--;
} else {
myadapter.getisselected().put(i, true);
checknum++;
}
}
// 刷新listview和textview的显示
datachanged();
}
});
// 取消按钮的回调接口
bt_deselectall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < list.size(); i++) {
if (myadapter.getisselected().get(i)) {
myadapter.getisselected().put(i, false);
checknum--;// 数量减1
}
}
// 刷新listview和textview的显示
datachanged();
}
});
// 绑定listview的监听器
lv.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
long arg3) {
// 取得viewholder对象,这样就省去了通过层层的findviewbyid去实例化我们需要的cb实例的步骤
viewholder holder = (viewholder) arg1.gettag();
// 改变checkbox的状态
holder.cb.toggle();
// 将checkbox的选中状况记录下来
myadapter.getisselected().put(arg2, holder.cb.ischecked());
// 调整选定条目
if (holder.cb.ischecked() == true) {
checknum++;
} else {
checknum--;
}
// 用textview显示
tv_show.settext("已选中" + checknum + "项");
}
});
}
// 初始化数据
private void initdate() {
for (int i = 0; i < 15; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和textview的显示
private void datachanged() {
// 通知listview刷新
madapter.notifydatasetchanged();
// textview显示最新的选中数目
tv_show.settext("已选中" + checknum + "项");
};
}
列表适配器:
[java]
package com.notice.listcheck;
import java.util.arraylist;
import java.util.hashmap;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.checkbox;
import android.widget.textview;
public class myadapter extends baseadapter {
// 填充数据的list
private arraylist<string> list;
// 用来控制checkbox的选中状况
private static hashmap<integer, boolean> isselected;
// 上下文
private context context;
// 用来导入布局
private layoutinflater inflater = null;
// 构造器
public myadapter(arraylist<string> list, context context) {
this.context = context;
this.list = list;
inflater = layoutinflater.from(context);
isselected = new hashmap<integer, boolean>();
// 初始化数据
initdate();
}
// 初始化isselected的数据
private void initdate() {
for (int i = 0; i < list.size(); i++) {
getisselected().put(i, false);
}
}
@override
public int getcount() {
return list.size();
}
@override
public object getitem(int position) {
return list.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) {
// 获得viewholder对象
holder = new viewholder();
// 导入布局并赋值给convertview
convertview = inflater.inflate(r.layout.listviewitem, null);
holder.tv = (textview) convertview.findviewbyid(r.id.item_tv);
holder.cb = (checkbox) convertview.findviewbyid(r.id.item_cb);
// 为view设置标签
convertview.settag(holder);
} else {
// 取出holder
holder = (viewholder) convertview.gettag();
}
// 设置list中textview的显示
holder.tv.settext(list.get(position));
// 根据isselected来设置checkbox的选中状况
holder.cb.setchecked(getisselected().get(position));
return convertview;
}
public static hashmap<integer, boolean> getisselected() {
return isselected;
}
public static void setisselected(hashmap<integer, boolean> isselected) {
myadapter.isselected = isselected;
}
public static class viewholder {
textview tv;
checkbox cb;
}
}
[html]
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<textview
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<linearlayout
android:id="@+id/line"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/tv"
android:orientation="horizontal" >
<button
android:id="@+id/bt_selectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="全选" />
<button
android:id="@+id/bt_cancleselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="反选" />
<button
android:id="@+id/bt_deselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="取消选择" />
</linearlayout>
<listview
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/line" />
</relativelayout>
listview 的item布局文件:
[html]
复制代码 代码如下:
<?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="fill_parent"
android:orientation="horizontal" >
<textview
android:id="@+id/item_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<checkbox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableintouchmode="false"
android:gravity="center_vertical" />
</linearlayout>
activity:
[java]
复制代码 代码如下:
public class ex_checkboxactivity extends activity {
private listview lv;
private myadapter madapter;
private arraylist<string> list;
private button bt_selectall;
private button bt_cancel;
private button bt_deselectall;
private int checknum; // 记录选中的条目数量
private textview tv_show;// 用于显示选中的条目数量
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
/* 实例化各个控件 */
lv = (listview) findviewbyid(r.id.lv);
bt_selectall = (button) findviewbyid(r.id.bt_selectall);
bt_cancel = (button) findviewbyid(r.id.bt_cancelselectall);
bt_deselectall = (button) findviewbyid(r.id.bt_deselectall);
tv_show = (textview) findviewbyid(r.id.tv);
list = new arraylist<string>();
// 为adapter准备数据
initdate();
// 实例化自定义的myadapter
madapter = new myadapter(list, this);
// 绑定adapter
lv.setadapter(madapter);
// 全选按钮的回调接口
bt_selectall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将myadapter中的map值全部设为true
for (int i = 0; i < list.size(); i++) {
myadapter.getisselected().put(i, true);
}
// 数量设为list的长度
checknum = list.size();
// 刷新listview和textview的显示
datachanged();
}
});
// 反选按钮的回调接口
bt_cancel.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将已选的设为未选,未选的设为已选
for (int i = 0; i < list.size(); i++) {
if (myadapter.getisselected().get(i)) {
myadapter.getisselected().put(i, false);
checknum--;
} else {
myadapter.getisselected().put(i, true);
checknum++;
}
}
// 刷新listview和textview的显示
datachanged();
}
});
// 取消按钮的回调接口
bt_deselectall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < list.size(); i++) {
if (myadapter.getisselected().get(i)) {
myadapter.getisselected().put(i, false);
checknum--;// 数量减1
}
}
// 刷新listview和textview的显示
datachanged();
}
});
// 绑定listview的监听器
lv.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
long arg3) {
// 取得viewholder对象,这样就省去了通过层层的findviewbyid去实例化我们需要的cb实例的步骤
viewholder holder = (viewholder) arg1.gettag();
// 改变checkbox的状态
holder.cb.toggle();
// 将checkbox的选中状况记录下来
myadapter.getisselected().put(arg2, holder.cb.ischecked());
// 调整选定条目
if (holder.cb.ischecked() == true) {
checknum++;
} else {
checknum--;
}
// 用textview显示
tv_show.settext("已选中" + checknum + "项");
}
});
}
// 初始化数据
private void initdate() {
for (int i = 0; i < 15; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和textview的显示
private void datachanged() {
// 通知listview刷新
madapter.notifydatasetchanged();
// textview显示最新的选中数目
tv_show.settext("已选中" + checknum + "项");
};
}
列表适配器:
[java]
复制代码 代码如下:
package com.notice.listcheck;
import java.util.arraylist;
import java.util.hashmap;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.checkbox;
import android.widget.textview;
public class myadapter extends baseadapter {
// 填充数据的list
private arraylist<string> list;
// 用来控制checkbox的选中状况
private static hashmap<integer, boolean> isselected;
// 上下文
private context context;
// 用来导入布局
private layoutinflater inflater = null;
// 构造器
public myadapter(arraylist<string> list, context context) {
this.context = context;
this.list = list;
inflater = layoutinflater.from(context);
isselected = new hashmap<integer, boolean>();
// 初始化数据
initdate();
}
// 初始化isselected的数据
private void initdate() {
for (int i = 0; i < list.size(); i++) {
getisselected().put(i, false);
}
}
@override
public int getcount() {
return list.size();
}
@override
public object getitem(int position) {
return list.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) {
// 获得viewholder对象
holder = new viewholder();
// 导入布局并赋值给convertview
convertview = inflater.inflate(r.layout.listviewitem, null);
holder.tv = (textview) convertview.findviewbyid(r.id.item_tv);
holder.cb = (checkbox) convertview.findviewbyid(r.id.item_cb);
// 为view设置标签
convertview.settag(holder);
} else {
// 取出holder
holder = (viewholder) convertview.gettag();
}
// 设置list中textview的显示
holder.tv.settext(list.get(position));
// 根据isselected来设置checkbox的选中状况
holder.cb.setchecked(getisselected().get(position));
return convertview;
}
public static hashmap<integer, boolean> getisselected() {
return isselected;
}
public static void setisselected(hashmap<integer, boolean> isselected) {
myadapter.isselected = isselected;
}
public static class viewholder {
textview tv;
checkbox cb;
}
}
推荐阅读
-
Android checkbox的listView(多选,全选,反选)具体实现方法
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
Android checkbox的listView(多选,全选,反选)具体实现方法
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
android 实现ListView嵌套Checkbox实现真正的多选、全选、反选、取消
-
两种不同的方法实现js对checkbox进行全选和反选_javascript技巧
-
android 实现ListView嵌套Checkbox实现真正的多选、全选、反选、取消
-
两种不同的方法实现js对checkbox进行全选和反选_javascript技巧
-
js实现checkbox全选、不选与反选的方法_javascript技巧
-
js实现checkbox全选、不选与反选的方法_javascript技巧