app的开发中,会常遇到这样的需求:批量取消(删除)list中的数据。这就要求listview支持批量选择、全选、单选等等功能,做一个比较强大的listview批量选择功能是很有必要的,那如何做呢?
可想而知,要支持批量选择,那checkbox的使用是不可或缺的,下面,就使用listview结合checkbox实现数据的批量选择。
先看下效果图,有图有真相:
先说明接下来要实现的listview+checkbox支持的功能:
- 1. 外部点击“编辑”(长按listview的某一项也可),出现复选框;
- 2. 支持全选、复选、全不选
- 3. 支持获取选中的数据的信息
接下来,带大家看下实现的步骤:
1. 定义list_item_data.xml,列表的内容显示,要求其中含有checkbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<?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:background= "#ffffff" android:orientation= "horizontal" > <linearlayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:gravity= "center_vertical" android:orientation= "horizontal" > <checkbox android:id= "@+id/checkbox_operate_data" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:layout_margin= "6dp" android:visibility= "gone" /> <imageview android:id= "@+id/material_item_img" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:adjustviewbounds= "true" android:scaletype= "centercrop" android:src= "@mipmap/ic_launcher" /> </linearlayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:orientation= "horizontal" android:paddingbottom= "10dp" android:paddingtop= "10dp" > <view android:id= "@+id/user_head_img" android:layout_width= "5dp" android:layout_height= "fill_parent" android:background= "#4483c9" /> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "vertical" > <textview android:id= "@+id/text_title" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:singleline= "true" android:text= "标题" android:textcolor= "#555555" android:textsize= "16sp" /> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "10dp" android:gravity= "center_vertical" android:orientation= "horizontal" > <textview android:id= "@+id/text_desc" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:gravity= "bottom" android:singleline= "true" android:text= "描述描述描述描述描述描述" android:textcolor= "#aaaaaa" android:textsize= "14sp" /> </linearlayout> </linearlayout> </linearlayout> </linearlayout> |
2. 定义数据显示的bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class databean { public string id; public string title; public string desc; public boolean ischeck; public databean(string id, string title, string desc) { this .id = id; this .title = title; this .desc = desc; } } |
注:databean中含有ischeck属性,该属性主要标志checkbox是否选中。
3. 定义数据显示的adapter,在该adapter中,我们需要实现两个重要的功能:控制是否显示checkbox; 控制checkbox是否显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
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; import java.util.list; public class myadapter extends baseadapter { private context mcontext; private list<databean> mdatas; private layoutinflater minflater; public boolean flage = false ; public myadapter(context mcontext, list<databean> mdatas) { this .mcontext = mcontext; this .mdatas = mdatas; minflater = layoutinflater.from( this .mcontext); } @override public int getcount() { return mdatas.size(); } @override public object getitem( int i) { return mdatas.get(i); } @override public long getitemid( int i) { return i; } @override public view getview( int position, view convertview, viewgroup viewgroup) { viewholder holder = null ; if (convertview == null ) { // 下拉项布局 convertview = minflater.inflate(r.layout.list_item_data, null ); holder = new viewholder(); holder.checkboxoperatedata = (checkbox) convertview.findviewbyid(r.id.checkbox_operate_data); holder.texttitle = (textview) convertview.findviewbyid(r.id.text_title); holder.textdesc = (textview) convertview.findviewbyid(r.id.text_desc); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } final databean databean = mdatas.get(position); if (databean != null ) { holder.texttitle.settext(databean.title); holder.textdesc.settext(databean.desc); // 根据isselected来设置checkbox的显示状况 if (flage) { holder.checkboxoperatedata.setvisibility(view.visible); } else { holder.checkboxoperatedata.setvisibility(view.gone); } holder.checkboxoperatedata.setchecked(databean.ischeck); //注意这里设置的不是oncheckedchanglistener,还是值得思考一下的 holder.checkboxoperatedata.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { if (databean.ischeck) { databean.ischeck = false ; } else { databean.ischeck = true ; } } }); } return convertview; } class viewholder { public checkbox checkboxoperatedata; public textview texttitle; public textview textdesc; } } |
注1: flage 字段,用于标志是否显示checkbox,通过在activity中改变该值,即可在getview方法中控制是否显示checkbox。
注2: 定义checkbox的setonclicklistener方法,而不是oncheckedchanglistener,如此使用,可方便控制checkbox是否选中。
4. 定义activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
public class mainactivity extends activity { private button button; private listview listview; private list<databean> mdatas; private myadapter madapter; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button) findviewbyid(r.id.button); listview = (listview) findviewbyid(r.id.listview); mdatas = new arraylist<>(); for ( int i = 0 ; i < 20 ; i++) { databean databean = new databean( "" + i, "上邪" , "山无棱,天地合,乃敢与君绝" ); mdatas.add(databean); } madapter = new myadapter( this , mdatas); listview.setadapter(madapter); } /** * 编辑、取消编辑 * @param view */ public void btneditlist(view view) { madapter.flage = !madapter.flage; if (madapter.flage) { button.settext( "取消" ); } else { button.settext( "编辑" ); } madapter.notifydatasetchanged(); } /** * 全选 * @param view */ public void btnselectalllist(view view) { if (madapter.flage) { for ( int i = 0 ; i < mdatas.size(); i++) { mdatas.get(i).ischeck = true ; } madapter.notifydatasetchanged(); } } /** * 全不选 * @param view */ public void btnnolist(view view) { if (madapter.flage) { for ( int i = 0 ; i < mdatas.size(); i++) { mdatas.get(i).ischeck = false ; } madapter.notifydatasetchanged(); } } /** * 反选 * @param view */ public void btnfanxuanlist(view view) { if (madapter.flage) { for ( int i = 0 ; i < mdatas.size(); i++) { if (mdatas.get(i).ischeck) { mdatas.get(i).ischeck = false ; } else { mdatas.get(i).ischeck = true ; } } madapter.notifydatasetchanged(); } } /** * 获取选中数据 * @param view */ public void btnoperatelist(view view) { list<string> ids = new arraylist<>(); if (madapter.flage) { for ( int i = 0 ; i < mdatas.size(); i++) { if (mdatas.get(i).ischeck) { ids.add(mdatas.get(i).id); } } toast.maketext(mainactivity. this ,ids.tostring(), toast.length_short).show(); log.e( "tag" , ids.tostring()); } } } |
如此这般,完美的可批量选择的listview便实现了,希望大家喜欢。