Android开发之ListView条目批量选择删除

简介:

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"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <ListView 
 8         
 9         android:id="@+id/list"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:layout_weight="1"
13         android:cacheColorHint="#FFF" >
14 
15         </ListView>
16         
17     <RelativeLayout 
18         
19         android:id="@+id/relative"
20         android:layout_width="fill_parent"
21         android:layout_height="50dp"
22         android:gravity="bottom"
23         android:background="@color/lemonchiffon"
24         android:visibility="gone"
25         >
26         <Button 
27             
28             android:id="@+id/cancle"
29             android:layout_width="wrap_content"
30             android:layout_height="wrap_content"
31             android:text="撤销    |"
32             android:textSize="20sp"
33             android:background="@null"
34             android:layout_centerVertical="true"
35             
36             />
37         <TextView 
38             
39             android:id="@+id/txtcount"
40             android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
42             android:text="共计"
43             android:textSize="15sp"
44             android:layout_centerInParent="true"
45             
46             />
47         
48         <Button 
49             
50             android:id="@+id/delete"
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:text="|  删除"
54             android:textSize="20sp"
55             android:background="@null"
56             android:layout_alignParentRight="true"
57             android: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"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6    
 7     <TextView
 8         android:id="@+id/txtName"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:layout_alignParentLeft="true"
12         android:layout_centerVertical="true"
13         android:layout_gravity="center_vertical"
14         android:layout_marginLeft="5dp"
15         android:layout_weight="1"
16         android:text="444444444444"
17         android:textSize="17sp"
18         android:textColor="#333" />
19     
20     <CheckBox 
21         android:id="@+id/check"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:visibility="gone"
25         android:clickable="false"
26         />
27 </LinearLayout>
复制代码

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

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

 

 

相关文章
|
2天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
22 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
25天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
16天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
19天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
19天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
19 1
Android开发之使用OpenGL实现翻书动画
|
19天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
15 1
Android开发之OpenGL的画笔工具GL10
|
25天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0
|
25天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
27天前
|
缓存 Java Android开发
安卓应用开发中的内存优化策略
在移动应用开发领域,性能一直是衡量应用质量的重要指标之一。特别是对于安卓平台,由于设备的硬件配置多样化,内存管理成为开发者面临的重大挑战。本文将深入探讨针对安卓平台的内存优化技巧,包括内存泄漏的预防、合理使用数据结构和算法、以及高效的资源释放机制。通过这些方法,开发者可以显著提升应用的性能和用户体验。
|
1月前
|
Java Android开发
Android开发系列全套课程
本系列课程面向有java基础,想进入企业从事android开发的计算机专业者。学习搭配实战案例,高效掌握岗位知识。
18 1