ListView实现的列表,如果是可编辑,可删除的,一般都要提供批量删除功能,否则的话,一项一项的删除体验很不好,也给用户带来了很大的麻烦。
实现效果图
具体实现代码
select.xml
主布局文件包含一个ListView还有一个隐藏的布局,包含了两个Button一个TextView,默认布局为gone,当监听到长按响应事件时候显示。
复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="match_parent"
4android:layout_height="match_parent"
5android:orientation="vertical" >
6
7<ListView
8
9android:id="@+id/list"
10android:layout_width="match_parent"
11android:layout_height="match_parent"
12android:layout_weight="1"
13android:cacheColorHint="#FFF" >
14
15</ListView>
16
17<RelativeLayout
18
19android:id="@+id/relative"
20android:layout_width="fill_parent"
21android:layout_height="50dp"
22android:gravity="bottom"
23android:background="@color/lemonchiffon"
24android:visibility="gone"
25>
26<Button
27
28android:id="@+id/cancle"
29android:layout_width="wrap_content"
30android:layout_height="wrap_content"
31android:text="撤销|"
32android:textSize="20sp"
33android:background="@null"
34android:layout_centerVertical="true"
35
36/>
37<TextView
38
39android:id="@+id/txtcount"
40android:layout_width="wrap_content"
41android:layout_height="wrap_content"
42android:text="共计"
43android:textSize="15sp"
44android:layout_centerInParent="true"
45
46/>
47
48<Button
49
50android:id="@+id/delete"
51android:layout_width="wrap_content"
52android:layout_height="wrap_content"
53android:text="|删除"
54android:textSize="20sp"
55android:background="@null"
56android:layout_alignParentRight="true"
57android:layout_centerVertical="true"
58/>
59
60
61</RelativeLayout>
62 </LinearLayout>
复制代码
item.xml
包含一个TextView 一个CheckBox
复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="match_parent"
4android:layout_height="match_parent"
5android:orientation="horizontal" >
6
7<TextView
8android:id="@+id/txtName"
9android:layout_width="match_parent"
10android:layout_height="wrap_content"
11android:layout_alignParentLeft="true"
12android:layout_centerVertical="true"
13android:layout_gravity="center_vertical"
14android:layout_marginLeft="5dp"
15android:layout_weight="1"
16android:text="444444444444"
17android:textSize="17sp"
18android:textColor="#333" />
19
20<CheckBox
21android:id="@+id/check"
22android:layout_width="wrap_content"
23android:layout_height="wrap_content"
24android:visibility="gone"
25android:clickable="false"
26/>
27 </LinearLayout>
复制代码
通过自定义Adapter绑定ListView数据源,实现长按监听,在长按监听时候,切记将监听事件返回ture。
复制代码
1 /**
2* @author ieasy360_1
3* 自定义Adapter
4*/
5class Adapter extends BaseAdapter{
6private Context context;
7private LayoutInflater inflater=null;
8private HashMap<Integer, View> mView ;
9publicHashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox
10publicHashMap<Integer, Boolean> ischeck;
11private TextView txtcount;
12public Adapter(Context context,TextView txtcount)
13{
14this.context = context;
15this.txtcount = txtcount;
16inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
17mView = new HashMap<Integer, View>();
18visiblecheck = new HashMap<Integer, Integer>();
19ischeck= new HashMap<Integer, Boolean>();
20if(isMulChoice){
21for(int i=0;i<array.size();i++){
22ischeck.put(i, false);
23visiblecheck.put(i, CheckBox.VISIBLE);
24}
25}else{
26for(int i=0;i<array.size();i++)
27{
28ischeck.put(i, false);
29visiblecheck.put(i, CheckBox.INVISIBLE);
30}
31}
32}
33
34public int getCount() {
35// TODO Auto-generated method stub
36return array.size();
37}
38
39public Object getItem(int position) {
40// TODO Auto-generated method stub
41return array.get(position);
42}
43
44public long getItemId(int position) {
45// TODO Auto-generated method stub
46return 0;
47}
48
49public View getView(final int position, View convertView, ViewGroup parent) {
50// TODO Auto-generated method stub
51View view = mView.get(position);
52if(view==null)
53{
54view = inflater.inflate(R.layout.item, null);
55TextView txt = (TextView)view.findViewById(R.id.txtName);
56final CheckBox ceb = (CheckBox)view.findViewById(R.id.check);
57
58txt.setText(array.get(position));
59
60ceb.setChecked(ischeck.get(position));
61ceb.setVisibility(visiblecheck.get(position));
62
63view.setOnLongClickListener(new Onlongclick());
64
65view.setOnClickListener(new OnClickListener() {
66
67public void onClick(View v) {
68// TODO Auto-generated method stub
69if(isMulChoice){
70if(ceb.isChecked()){
71ceb.setChecked(false);
72selectid.remove(array.get(position));
73}else{
74ceb.setChecked(true);
75selectid.add(array.get(position));
76}
77txtcount.setText("共选择了"+selectid.size()+"项");
78}else {
79Toast.makeText(context, "点击了"+array.get(position), Toast.LENGTH_LONG).show();
80}
81}
82});
83
84mView.put(position, view);
85}
86return view;
87}
88
89class Onlongclick implements OnLongClickListener{
90
91public boolean onLongClick(View v) {
92// TODO Auto-generated method stub
93
94isMulChoice = true;
95selectid.clear();
96layout.setVisibility(View.VISIBLE);
97for(int i=0;i<array.size();i++)
98{
99adapter.visiblecheck.put(i, CheckBox.VISIBLE);
100}
101adapter = new Adapter(context,txtcount);
102listview.setAdapter(adapter);
103return true;
104}
105}
106}
复制代码
全部实现代码
复制代码
1 package com.example.test;
2 import java.util.ArrayList;
3 import java.util.HashMap;
4 import java.util.List;
5 import android.app.Activity;
6 import android.content.Context;
7 import android.os.Bundle;
8 import android.view.ContextMenu;
9 import android.view.ContextMenu.ContextMenuInfo;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.view.View.OnLongClickListener;
14 import android.view.ViewGroup;
15 import android.widget.BaseAdapter;
16 import android.widget.Button;
17 import android.widget.CheckBox;
18 import android.widget.ListView;
19 import android.widget.RelativeLayout;
20 import android.widget.TextView;
21 import android.widget.Toast;
22
23 /**
24* @author ieasy360_1
25*
26*/
27 public class MulSelect extends Activity implements OnClickListener {
28
29private ListView listview;
30private Contextcontext;
31private List<String> array = new ArrayList<String>();
32private List<String> selectid = new ArrayList<String>();
33private boolean isMulChoice = false; //是否多选
34private Adapteradapter;
35private RelativeLayout layout;
36private Button cancle,delete;
37private TextView txtcount;
38
39@Override
40protected void onCreate(Bundle savedInstanceState) {
41// TODO Auto-generated method stub
42super.onCreate(savedInstanceState);
43setContentView(R.layout.select);
44context = this;
45listview = (ListView)findViewById(R.id.list);
46layout = (RelativeLayout)findViewById(R.id.relative);
47txtcount = (TextView)findViewById(R.id.txtcount);
48cancle= (Button)findViewById(R.id.cancle);
49delete= (Button)findViewById(R.id.delete);
50cancle.setOnClickListener(this);
51delete.setOnClickListener(this);
52init();
53adapter = new Adapter(context,txtcount);
54listview.setAdapter(adapter);
55
56}
57
58void init()
59{
60for(int i=0;i<20;i++)
61{
62array.add("小明"+i);
63}
64}
65
66public void onClick(View v) {
67// TODO Auto-generated method stub
68switch (v.getId()) {
69case R.id.cancle:
70isMulChoice = false;
71selectid.clear();
72adapter = new Adapter(context,txtcount);
73listview.setAdapter(adapter);
74layout.setVisibility(View.INVISIBLE);
75break;
76case R.id.delete:
77isMulChoice =false;
78for(int i=0;i<selectid.size();i++){
79for(int j=0;j<array.size();j++){
80if(selectid.get(i).equals(array.get(j))){
81array.remove(j);
82}
83}
84}
85selectid.clear();
86adapter = new Adapter(context,txtcount);
87listview.setAdapter(adapter);
88layout.setVisibility(View.INVISIBLE);
89break;
90default:
91break;
92}
93
94}
95
96@Override
97public void onCreateContextMenu(ContextMenu menu, View v,
98ContextMenuInfo menuInfo) {
99// TODO Auto-generated method stub
100super.onCreateContextMenu(menu, v, menuInfo);
101menu.setHeaderTitle("操作");
102}
103
104/**
105* @author ieasy360_1
106* 自定义Adapter
107*/
108class Adapter extends BaseAdapter{
109private Context context;
110private LayoutInflater inflater=null;
111private HashMap<Integer, View> mView ;
112publicHashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox
113publicHashMap<Integer, Boolean> ischeck;
114private TextView txtcount;
115public Adapter(Context context,TextView txtcount)
116{
117this.context = context;
118this.txtcount = txtcount;
119inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
120mView = new HashMap<Integer, View>();
121visiblecheck = new HashMap<Integer, Integer>();
122ischeck= new HashMap<Integer, Boolean>();
123if(isMulChoice){
124for(int i=0;i<array.size();i++){
125ischeck.put(i, false);
126visiblecheck.put(i, CheckBox.VISIBLE);
127}
128}else{
129for(int i=0;i<array.size();i++)
130{
131ischeck.put(i, false);
132visiblecheck.put(i, CheckBox.INVISIBLE);
133}
134}
135}
136
137public int getCount() {
138// TODO Auto-generated method stub
139return array.size();
140}
141
142public Object getItem(int position) {
143// TODO Auto-generated method stub
144return array.get(position);
145}
146
147public long getItemId(int position) {
148// TODO Auto-generated method stub
149return 0;
150}
151
152public View getView(final int position, View convertView, ViewGroup parent) {
153// TODO Auto-generated method stub
154View view = mView.get(position);
155if(view==null)
156{
157view = inflater.inflate(R.layout.item, null);
158TextView txt = (TextView)view.findViewById(R.id.txtName);
159final CheckBox ceb = (CheckBox)view.findViewById(R.id.check);
160
161txt.setText(array.get(position));
162
163ceb.setChecked(ischeck.get(position));
164ceb.setVisibility(visiblecheck.get(position));
165
166view.setOnLongClickListener(new Onlongclick());
167
168view.setOnClickListener(new OnClickListener() {
169
170public void onClick(View v) {
171// TODO Auto-generated method stub
172if(isMulChoice){
173if(ceb.isChecked()){
174ceb.setChecked(false);
175selectid.remove(array.get(position));
176}else{
177ceb.setChecked(true);
178selectid.add(array.get(position));
179}
180txtcount.setText("共选择了"+selectid.size()+"项");
181}else {
182Toast.makeText(context, "点击了"+array.get(position), Toast.LENGTH_LONG).show();
183}
184}
185});
186
187mView.put(position, view);
188}
189return view;
190}
191
192class Onlongclick implements OnLongClickListener{
193
194public boolean onLongClick(View v) {
195// TODO Auto-generated method stub
196
197isMulChoice = true;
198selectid.clear();
199layout.setVisibility(View.VISIBLE);
200for(int i=0;i<array.size();i++)
201{
202adapter.visiblecheck.put(i, CheckBox.VISIBLE);
203}
204adapter = new Adapter(context,txtcount);
205listview.setAdapter(adapter);
206return true;
207}
208}
209}
210 }