ListView实现的列表,假设是可编辑,可删除的,一般都要提供批量删除功能,否则的话,一项一项的删除体验非常不好,也给用户带来了非常大的麻烦。

实现效果图

详细实现代码

select.xml

主布局文件包括一个ListView另一个隐藏的布局,包括了两个Button一个TextView,默认布局为gone,当监听到长按响应事件时候显示。

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:orientation="vertical" >
  6. 6
  7. 7 <ListView
  8. 8
  9. 9 android:id="@+id/list"
  10. 10 android:layout_width="match_parent"
  11. 11 android:layout_height="match_parent"
  12. 12 android:layout_weight="1"
  13. 13 android:cacheColorHint="#FFF" >
  14. 14
  15. 15 </ListView>
  16. 16
  17. 17 <RelativeLayout
  18. 18
  19. 19 android:id="@+id/relative"
  20. 20 android:layout_width="fill_parent"
  21. 21 android:layout_height="50dp"
  22. 22 android:gravity="bottom"
  23. 23 android:background="@color/lemonchiffon"
  24. 24 android:visibility="gone"
  25. 25 >
  26. 26 <Button
  27. 27
  28. 28 android:id="@+id/cancle"
  29. 29 android:layout_width="wrap_content"
  30. 30 android:layout_height="wrap_content"
  31. 31 android:text="撤销 |"
  32. 32 android:textSize="20sp"
  33. 33 android:background="@null"
  34. 34 android:layout_centerVertical="true"
  35. 35
  36. 36 />
  37. 37 <TextView
  38. 38
  39. 39 android:id="@+id/txtcount"
  40. 40 android:layout_width="wrap_content"
  41. 41 android:layout_height="wrap_content"
  42. 42 android:text="共计"
  43. 43 android:textSize="15sp"
  44. 44 android:layout_centerInParent="true"
  45. 45
  46. 46 />
  47. 47
  48. 48 <Button
  49. 49
  50. 50 android:id="@+id/delete"
  51. 51 android:layout_width="wrap_content"
  52. 52 android:layout_height="wrap_content"
  53. 53 android:text="| 删除"
  54. 54 android:textSize="20sp"
  55. 55 android:background="@null"
  56. 56 android:layout_alignParentRight="true"
  57. 57 android:layout_centerVertical="true"
  58. 58 />
  59. 59
  60. 60
  61. 61 </RelativeLayout>
  62. 62 </LinearLayout>

item.xml

包括一个TextView 一个CheckBox

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:orientation="horizontal" >
  6. 6
  7. 7 <TextView
  8. 8 android:id="@+id/txtName"
  9. 9 android:layout_width="match_parent"
  10. 10 android:layout_height="wrap_content"
  11. 11 android:layout_alignParentLeft="true"
  12. 12 android:layout_centerVertical="true"
  13. 13 android:layout_gravity="center_vertical"
  14. 14 android:layout_marginLeft="5dp"
  15. 15 android:layout_weight="1"
  16. 16 android:text="444444444444"
  17. 17 android:textSize="17sp"
  18. 18 android:textColor="#333" />
  19. 19
  20. 20 <CheckBox
  21. 21 android:id="@+id/check"
  22. 22 android:layout_width="wrap_content"
  23. 23 android:layout_height="wrap_content"
  24. 24 android:visibility="gone"
  25. 25 android:clickable="false"
  26. 26 />
  27. 27 </LinearLayout>

通过自己定义Adapter绑定ListView数据源,实现长按监听,在长按监听时候,切记将监听事件返回ture。

  1. 1 /**
  2. 2 * @author ieasy360_1
  3. 3 * 自己定义Adapter
  4. 4 */
  5. 5 class Adapter extends BaseAdapter{
  6. 6 private Context context;
  7. 7 private LayoutInflater inflater=null;
  8. 8 private HashMap<Integer, View> mView ;
  9. 9 public HashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox
  10. 10 public HashMap<Integer, Boolean> ischeck;
  11. 11 private TextView txtcount;
  12. 12 public Adapter(Context context,TextView txtcount)
  13. 13 {
  14. 14 this.context = context;
  15. 15 this.txtcount = txtcount;
  16. 16 inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  17. 17 mView = new HashMap<Integer, View>();
  18. 18 visiblecheck = new HashMap<Integer, Integer>();
  19. 19 ischeck = new HashMap<Integer, Boolean>();
  20. 20 if(isMulChoice){
  21. 21 for(int i=0;i<array.size();i++){
  22. 22 ischeck.put(i, false);
  23. 23 visiblecheck.put(i, CheckBox.VISIBLE);
  24. 24 }
  25. 25 }else{
  26. 26 for(int i=0;i<array.size();i++)
  27. 27 {
  28. 28 ischeck.put(i, false);
  29. 29 visiblecheck.put(i, CheckBox.INVISIBLE);
  30. 30 }
  31. 31 }
  32. 32 }
  33. 33
  34. 34 public int getCount() {
  35. 35 // TODO Auto-generated method stub
  36. 36 return array.size();
  37. 37 }
  38. 38
  39. 39 public Object getItem(int position) {
  40. 40 // TODO Auto-generated method stub
  41. 41 return array.get(position);
  42. 42 }
  43. 43
  44. 44 public long getItemId(int position) {
  45. 45 // TODO Auto-generated method stub
  46. 46 return 0;
  47. 47 }
  48. 48
  49. 49 public View getView(final int position, View convertView, ViewGroup parent) {
  50. 50 // TODO Auto-generated method stub
  51. 51 View view = mView.get(position);
  52. 52 if(view==null)
  53. 53 {
  54. 54 view = inflater.inflate(R.layout.item, null);
  55. 55 TextView txt = (TextView)view.findViewById(R.id.txtName);
  56. 56 final CheckBox ceb = (CheckBox)view.findViewById(R.id.check);
  57. 57
  58. 58 txt.setText(array.get(position));
  59. 59
  60. 60 ceb.setChecked(ischeck.get(position));
  61. 61 ceb.setVisibility(visiblecheck.get(position));
  62. 62
  63. 63 view.setOnLongClickListener(new Onlongclick());
  64. 64
  65. 65 view.setOnClickListener(new OnClickListener() {
  66. 66
  67. 67 public void onClick(View v) {
  68. 68 // TODO Auto-generated method stub
  69. 69 if(isMulChoice){
  70. 70 if(ceb.isChecked()){
  71. 71 ceb.setChecked(false);
  72. 72 selectid.remove(array.get(position));
  73. 73 }else{
  74. 74 ceb.setChecked(true);
  75. 75 selectid.add(array.get(position));
  76. 76 }
  77. 77 txtcount.setText("共选择了"+selectid.size()+"项");
  78. 78 }else {
  79. 79 Toast.makeText(context, "点击了"+array.get(position), Toast.LENGTH_LONG).show();
  80. 80 }
  81. 81 }
  82. 82 });
  83. 83
  84. 84 mView.put(position, view);
  85. 85 }
  86. 86 return view;
  87. 87 }
  88. 88
  89. 89 class Onlongclick implements OnLongClickListener{
  90. 90
  91. 91 public boolean onLongClick(View v) {
  92. 92 // TODO Auto-generated method stub
  93. 93
  94. 94 isMulChoice = true;
  95. 95 selectid.clear();
  96. 96 layout.setVisibility(View.VISIBLE);
  97. 97 for(int i=0;i<array.size();i++)
  98. 98 {
  99. 99 adapter.visiblecheck.put(i, CheckBox.VISIBLE);
  100. 100 }
  101. 101 adapter = new Adapter(context,txtcount);
  102. 102 listview.setAdapter(adapter);
  103. 103 return true;
  104. 104 }
  105. 105 }
  106. 106 }

所有实现代码

  1. 1 package com.example.test;
  2. 2 import java.util.ArrayList;
  3. 3 import java.util.HashMap;
  4. 4 import java.util.List;
  5. 5 import android.app.Activity;
  6. 6 import android.content.Context;
  7. 7 import android.os.Bundle;
  8. 8 import android.view.ContextMenu;
  9. 9 import android.view.ContextMenu.ContextMenuInfo;
  10. 10 import android.view.LayoutInflater;
  11. 11 import android.view.View;
  12. 12 import android.view.View.OnClickListener;
  13. 13 import android.view.View.OnLongClickListener;
  14. 14 import android.view.ViewGroup;
  15. 15 import android.widget.BaseAdapter;
  16. 16 import android.widget.Button;
  17. 17 import android.widget.CheckBox;
  18. 18 import android.widget.ListView;
  19. 19 import android.widget.RelativeLayout;
  20. 20 import android.widget.TextView;
  21. 21 import android.widget.Toast;
  22. 22
  23. 23 /**
  24. 24 * @author ieasy360_1
  25. 25 *
  26. 26 */
  27. 27 public class MulSelect extends Activity implements OnClickListener {
  28. 28
  29. 29 private ListView listview;
  30. 30 private Context context;
  31. 31 private List<String> array = new ArrayList<String>();
  32. 32 private List<String> selectid = new ArrayList<String>();
  33. 33 private boolean isMulChoice = false; //是否多选
  34. 34 private Adapter adapter;
  35. 35 private RelativeLayout layout;
  36. 36 private Button cancle,delete;
  37. 37 private TextView txtcount;
  38. 38
  39. 39 @Override
  40. 40 protected void onCreate(Bundle savedInstanceState) {
  41. 41 // TODO Auto-generated method stub
  42. 42 super.onCreate(savedInstanceState);
  43. 43 setContentView(R.layout.select);
  44. 44 context = this;
  45. 45 listview = (ListView)findViewById(R.id.list);
  46. 46 layout = (RelativeLayout)findViewById(R.id.relative);
  47. 47 txtcount = (TextView)findViewById(R.id.txtcount);
  48. 48 cancle = (Button)findViewById(R.id.cancle);
  49. 49 delete = (Button)findViewById(R.id.delete);
  50. 50 cancle.setOnClickListener(this);
  51. 51 delete.setOnClickListener(this);
  52. 52 init();
  53. 53 adapter = new Adapter(context,txtcount);
  54. 54 listview.setAdapter(adapter);
  55. 55
  56. 56 }
  57. 57
  58. 58 void init()
  59. 59 {
  60. 60 for(int i=0;i<20;i++)
  61. 61 {
  62. 62 array.add("小明"+i);
  63. 63 }
  64. 64 }
  65. 65
  66. 66 public void onClick(View v) {
  67. 67 // TODO Auto-generated method stub
  68. 68 switch (v.getId()) {
  69. 69 case R.id.cancle:
  70. 70 isMulChoice = false;
  71. 71 selectid.clear();
  72. 72 adapter = new Adapter(context,txtcount);
  73. 73 listview.setAdapter(adapter);
  74. 74 layout.setVisibility(View.INVISIBLE);
  75. 75 break;
  76. 76 case R.id.delete:
  77. 77 isMulChoice =false;
  78. 78 for(int i=0;i<selectid.size();i++){
  79. 79 for(int j=0;j<array.size();j++){
  80. 80 if(selectid.get(i).equals(array.get(j))){
  81. 81 array.remove(j);
  82. 82 }
  83. 83 }
  84. 84 }
  85. 85 selectid.clear();
  86. 86 adapter = new Adapter(context,txtcount);
  87. 87 listview.setAdapter(adapter);
  88. 88 layout.setVisibility(View.INVISIBLE);
  89. 89 break;
  90. 90 default:
  91. 91 break;
  92. 92 }
  93. 93
  94. 94 }
  95. 95
  96. 96 @Override
  97. 97 public void onCreateContextMenu(ContextMenu menu, View v,
  98. 98 ContextMenuInfo menuInfo) {
  99. 99 // TODO Auto-generated method stub
  100. 100 super.onCreateContextMenu(menu, v, menuInfo);
  101. 101 menu.setHeaderTitle("操作");
  102. 102 }
  103. 103
  104. 104 /**
  105. 105 * @author ieasy360_1
  106. 106 * 自己定义Adapter
  107. 107 */
  108. 108 class Adapter extends BaseAdapter{
  109. 109 private Context context;
  110. 110 private LayoutInflater inflater=null;
  111. 111 private HashMap<Integer, View> mView ;
  112. 112 public HashMap<Integer, Integer> visiblecheck ;//用来记录是否显示checkBox
  113. 113 public HashMap<Integer, Boolean> ischeck;
  114. 114 private TextView txtcount;
  115. 115 public Adapter(Context context,TextView txtcount)
  116. 116 {
  117. 117 this.context = context;
  118. 118 this.txtcount = txtcount;
  119. 119 inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  120. 120 mView = new HashMap<Integer, View>();
  121. 121 visiblecheck = new HashMap<Integer, Integer>();
  122. 122 ischeck = new HashMap<Integer, Boolean>();
  123. 123 if(isMulChoice){
  124. 124 for(int i=0;i<array.size();i++){
  125. 125 ischeck.put(i, false);
  126. 126 visiblecheck.put(i, CheckBox.VISIBLE);
  127. 127 }
  128. 128 }else{
  129. 129 for(int i=0;i<array.size();i++)
  130. 130 {
  131. 131 ischeck.put(i, false);
  132. 132 visiblecheck.put(i, CheckBox.INVISIBLE);
  133. 133 }
  134. 134 }
  135. 135 }
  136. 136
  137. 137 public int getCount() {
  138. 138 // TODO Auto-generated method stub
  139. 139 return array.size();
  140. 140 }
  141. 141
  142. 142 public Object getItem(int position) {
  143. 143 // TODO Auto-generated method stub
  144. 144 return array.get(position);
  145. 145 }
  146. 146
  147. 147 public long getItemId(int position) {
  148. 148 // TODO Auto-generated method stub
  149. 149 return 0;
  150. 150 }
  151. 151
  152. 152 public View getView(final int position, View convertView, ViewGroup parent) {
  153. 153 // TODO Auto-generated method stub
  154. 154 View view = mView.get(position);
  155. 155 if(view==null)
  156. 156 {
  157. 157 view = inflater.inflate(R.layout.item, null);
  158. 158 TextView txt = (TextView)view.findViewById(R.id.txtName);
  159. 159 final CheckBox ceb = (CheckBox)view.findViewById(R.id.check);
  160. 160
  161. 161 txt.setText(array.get(position));
  162. 162
  163. 163 ceb.setChecked(ischeck.get(position));
  164. 164 ceb.setVisibility(visiblecheck.get(position));
  165. 165
  166. 166 view.setOnLongClickListener(new Onlongclick());
  167. 167
  168. 168 view.setOnClickListener(new OnClickListener() {
  169. 169
  170. 170 public void onClick(View v) {
  171. 171 // TODO Auto-generated method stub
  172. 172 if(isMulChoice){
  173. 173 if(ceb.isChecked()){
  174. 174 ceb.setChecked(false);
  175. 175 selectid.remove(array.get(position));
  176. 176 }else{
  177. 177 ceb.setChecked(true);
  178. 178 selectid.add(array.get(position));
  179. 179 }
  180. 180 txtcount.setText("共选择了"+selectid.size()+"项");
  181. 181 }else {
  182. 182 Toast.makeText(context, "点击了"+array.get(position), Toast.LENGTH_LONG).show();
  183. 183 }
  184. 184 }
  185. 185 });
  186. 186
  187. 187 mView.put(position, view);
  188. 188 }
  189. 189 return view;
  190. 190 }
  191. 191
  192. 192 class Onlongclick implements OnLongClickListener{
  193. 193
  194. 194 public boolean onLongClick(View v) {
  195. 195 // TODO Auto-generated method stub
  196. 196
  197. 197 isMulChoice = true;
  198. 198 selectid.clear();
  199. 199 layout.setVisibility(View.VISIBLE);
  200. 200 for(int i=0;i<array.size();i++)
  201. 201 {
  202. 202 adapter.visiblecheck.put(i, CheckBox.VISIBLE);
  203. 203 }
  204. 204 adapter = new Adapter(context,txtcount);
  205. 205 listview.setAdapter(adapter);
  206. 206 return true;
  207. 207 }
  208. 208 }
  209. 209 }
  210. 210 }

Android开发之ListView条目批量选择删除的更多相关文章

  1. 【转】Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法

    Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法 [原文链接] 这篇文章完美的解决了我几个月没结论的bug... 感谢热爱分享的技术达人~ 我是怎么走进这个大坑的 ...

  2. Android开发之ListView实现不同品种分类分隔栏的效果(非ExpandableListView实现)

    我们有时候会遇到这么一个情况.就是我在一个ListView里面须要显示的东西事实上是有种类之分的.比方我要分冬天,夏天.秋天.春天,然后在这每一个季节以下再去载入各自的条目数据. 还有,比方我们的通讯 ...

  3. android 开发之 ListView 与Adapter 应用实践

    在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...

  4. Android开发之ListView添加多种布局效果演示

    在这个案例中展示的新闻列表,使用到ListView控件,然后在适配器中添加多种布局效果,这里通过重写BaseAdapter类中的 getViewType()和getItemViewType()来做判断 ...

  5. Android开发之ListView设置隔行变色

    public class HLCheckAdapter extends BaseAdapter { private List<HuoLiang> list; private Context ...

  6. Android开发之ListView详解 以及简单的listView优化

    ListView列表视图 最常用的控件之一,使用场景例如:微信,手机QQ等等. android:divider:每个item之间的分割线,可以使用图片或者色值. android:dividerHeig ...

  7. android开发之 listview中的item去掉分割线 隐藏分割线

    有三种方法: 1> 设置android:divider="@null" 2> android:divider="#00000000" #000000 ...

  8. Android开发之AlertDialog警告提示框删除与取消 详解代码

    package cc.jiusansec.www; import android.app.Activity; import android.app.AlertDialog; import androi ...

  9. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

随机推荐

  1. 一步一步学习IdentityServer4 (7) IdentityServer4成功配置全部配置

    auth.liyouming.com 全部配 public class Startup { public Startup(IConfiguration configuration) { Configu ...

  2. 微信小程序实现左滑删除源码

    左滑删除效果在app的交互方式中十分流行,比如全民应用微信 微信左滑删除 再比如曾引起很大反响的效率app Clear Clear左滑删除 从技术上来说,实现这个效果并不困难,响应一下滑动操作,移动一 ...

  3. ASP.NET MVC下判断用户登录和授权状态方法

    在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在a ...

  4. Linux下Github的使用方法

    1 Linux下Git和GitHub环境的搭建 安装Git, 使用命令sudo apt-get install git 创建GitHub帐号 生成ssh key,使用命令 ssh-keygen -t ...

  5. Bootstrap入门八:图片

    1.响应式图片 在 Bootstrap 版本 3 中,通过为图片添加 .img-responsive 类可以让图片支持响应式布局.其实质是为图片设置了 max-width: 100%;. height ...

  6. 你的跑步姿势正确吗? 教你正确跑步姿势 & 常识

    转载!!!!!搞IT必须运动一下 前言: 最近两年跑步的人越来越多,跑步在大部分人的观念中都是毫无技术含量,只要迈开腿就行了,其实这也是造成大多数跑步人士伤病的根源.对跑步的认知不足,跑步是一项看起来 ...

  7. [BZOJ3309]DZY Loves Math(莫比乌斯反演+线性筛)

    $\sum\limits_{T=1}^{n}\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d|T}f(d)\mu(\fr ...

  8. 【持续更新】NOIP注意事项

    1.无根据的乱搞不可能对 2.必须造极限数据跑一下 3.必须测空间 4.不管用不用都把cstring加上 5.开文件测样例 6.删一长串代码最好注释 7.到10:00先敲暴力 8.题读三遍 9.先做好 ...

  9. [HDU6203]ping ping ping

    题目大意: 给你一棵树,其中有一些点是坏掉的.告诉你k个点对表示这两个点的路径上至少有1个点是坏掉的.问整棵树上至少有多少点是坏的. 思路: 贪心. 找出每组点对的LCA,对所有点对按照LCA的深度排 ...

  10. Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫

    从Apache2.2.X到Apache2.4.X,在配置上稍微有点不同,需要特别注意.现在记录下关于访问控制的配置. 经过苦苦搜索,终于配置成功.参考了这篇文章:http://www.cnblogs. ...