安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
ListView 自身提供了 CheckBox 只需要添加一行代码
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
但是这种实现想要自己控制操作起来局限很多。所以我选择了自己添加CheckBox的方式。可以支持列表项的全选,删除,并保持数据的对应关系不会乱。
列表中的CheckBox选中状态与一个Map进行绑定,利用 adapter.notifyDataSetChanged();来更新界面。
效果如下:
下面直接看代码把。
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#C9F1FF">
- <ListView
- android:id="@id/android:list"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:fadingEdge="none"
- android:cacheColorHint="#00000000"/>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="40.0dip"
- android:layout_alignParentBottom="true">
- <CheckBox android:id="@+id/all_check_btn"
- android:layout_width="40.0dip"
- android:background="@drawable/bottom_back_bg"
- android:layout_height="40.0dip"
- android:layout_alignParentLeft="true"/>
- </RelativeLayout>
- </RelativeLayout>
item.xml
- <?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="wrap_content"
- android:layout_marginRight="3.0dip" android:layout_weight="1.0"
- android:orientation="horizontal" android:descendantFocusability="blocksDescendants">
- <CheckBox android:id="@+id/isCheakBox" android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_alignParentLeft="true" />
- <!-- 日报图片 -->
- <ImageView android:id="@+id/dailyPic" android:contentDescription="dailyPic"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_marginTop="3.0dip" android:src="@drawable/reports"
- android:layout_toRightOf="@id/isCheakBox" android:layout_centerVertical="true"/>
- <!--附件名称 -->
- <TextView
- android:id="@+id/dailyName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/dailyPic"
- android:text="日报名称" android:layout_centerVertical="true"
- android:textColor="#000000"
- android:textSize="12.0sp" />
- <ImageButton android:id="@+id/deleteAttachment"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_marginTop="3.0dip" android:background="@drawable/delete"
- android:layout_centerVertical="true" android:focusable="false"
- android:layout_alignParentRight="true" android:layout_marginRight="20dp"/>
- <!--附件名称 -->
- </RelativeLayout>
Activity代码
- public class ListViewCheckBoxActivity extends ListActivity {
- private static final String TAG = "ListViewCheckBoxActivity";
- private List<Item> itemList;
- private DraftDailyAdapter adapter;
- private Map<Integer, Boolean> isCheckedMap;
- private CheckBox allCheckBox;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- allCheckBox = (CheckBox)findViewById(R.id.all_check_btn);
- itemList = new ArrayList<Item>();
- isCheckedMap = new HashMap<Integer, Boolean>();
- //初始化数据
- for(int i=0;i<8;i++){
- Item item = new Item();
- item.id=i;
- item.name = "第"+i+"篇日报";
- itemList.add(item);
- isCheckedMap.put(i,false);
- }
- adapter = new DraftDailyAdapter(this,itemList);
- setListAdapter(adapter);
- allCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- Set<Integer> set = isCheckedMap.keySet();
- Iterator<Integer> iterator = set.iterator();
- if(isChecked){
- while(iterator.hasNext()){
- Integer keyId = iterator.next();
- isCheckedMap.put(keyId,true);
- }
- }else{
- while(iterator.hasNext()){
- Integer keyId = iterator.next();
- isCheckedMap.put(keyId,false);
- }
- }
- adapter.notifyDataSetChanged();
- }
- });
- }
- class DraftDailyAdapter extends BaseAdapter {
- public List<Item> list;
- private Context context;
- LayoutInflater inflater;
- public DraftDailyAdapter(Context context, List<Item> list) {
- super();
- this.list = list;
- this.context = context;
- inflater = LayoutInflater.from(this.context);
- }
- @Override
- public int getCount() {
- return list == null ? 0 : list.size();
- }
- @Override
- public Object getItem(int location) {
- return list.get(location);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- Item item = list.get(position);
- //Item的位置
- final int listPosition = position;
- //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态
- final int id = item.id;
- if(convertView == null){
- holder = new ViewHolder();
- convertView = inflater.inflate(R.layout.item, null);
- holder.tvName = (TextView)convertView.findViewById(R.id.dailyName);
- holder.deleteButton = (ImageButton)convertView.findViewById(R.id.deleteAttachment);
- holder.cBox = (CheckBox)convertView.findViewById(R.id.isCheakBox);
- convertView.setTag(holder);
- }else{
- holder = (ViewHolder) convertView.getTag();
- }
- Log.d(TAG, "id="+id);
- holder.cBox.setChecked(isCheckedMap.get(id));
- holder.tvName.setText(item.name);
- holder.deleteButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View paramView) {
- //Log.d(TAG, "deletePosition="+listPosition+"");
- //删除list中的数据
- list.remove(listPosition);
- //删除Map中对应选中状态数据
- isCheckedMap.remove(id);
- //通知列表数据修改
- adapter.notifyDataSetChanged();
- }
- });
- holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked){
- isCheckedMap.put(id,true);
- }else{
- isCheckedMap.put(id,false);
- }
- }
- });
- return convertView;
- }
- public final class ViewHolder {
- public TextView tvName;
- public ImageButton deleteButton;
- public CheckBox cBox;
- }
- }
- class Item {
- private Integer id;
- private String name;
- }
- }
资源文件见附件源代码。
安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)的更多相关文章
- 我的Android进阶之旅------>Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项
今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.所以首先我得把系统的语言切换到波兰语,问题是哪个是波兰语呢? 我还真的不认识哪个列表项代表着 ...
- 实现listview中checkbox的多选与记录
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- android UI进阶之实现listview中checkbox的多选与记录
今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...
- 【转】android UI进阶之实现listview中checkbox的多选与记录--不错
原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...
- android ListView中CheckBox错位的解决
貌似已经非常晚了,可是还是想记下笔记,想让今天完满. 在ListView中加了checkBox,但是发现点击改变其选中状态的时候,发现其位置错乱.状态改变的并非你选中的,百思不得其解.后面通过上网查资 ...
- ListView中CheckBox使用问题
因为CheckBox的点击事件优先级比ListView的高,所以当ListView中使用CheckBox会导致ListView的setOnItemClickListener失去响应. 解决的方法:在C ...
- 抛砖引玉:关于Android的ListView中CheckBox错乱
首先:参考了这篇翻译的文章:http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html 文章中关于说的Android中的Recyc ...
- Android 解决listview中checkBox错位选择
假如ListView,分成2页(或者设置数据可以纵向拉,可隐藏),每页3条数据,每个Listview的Item 里面有个checkBox,现在,当我选择第一页的前两天数据,翻到第二页,竟然第二页后两条 ...
- ListView中CheckBox错乱解决
思路: ListView在复用的时候会出现很多问题,CheckBox状态会出现错乱,解决思路: 1.使用Map集合的键值对的形式来存放position位置上CheckBox的状态 2.监听CheckB ...
随机推荐
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(六)针对spark2.2.1以yarn方式启动spark-shell抛出异常:ERROR cluster.YarnSchedulerBackend$YarnSchedulerEndpoint: Sending RequestExecutors(0,0,Map(),Set()) to AM was unsuccessful
Spark以yarn方式运行时抛出异常: [spark@master bin]$ cd /opt/spark--bin-hadoop2./bin [spark@master bin]$ ./spark ...
- c#:winform从一个toolstriptool上拖动一个图标到一个自定义usercontrol内。
效果: 在一个winform工程中,添加一个ToolStrip然后给它添加一个ToolStripButton(tsbStart,它就是红色框圈选的图标) this.toolStripTools = n ...
- 抽屉柜式MCC柜中PROFIBUS设备推荐波特率及相应传输距离
抽屉柜式MCC柜中PROFIBUS设备推荐波特率及相应传输距离.例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L ...
- .Net C# 5.0 规范:迭代器
本文内容 枚举器 enumerator 接口 - IEnumerator 可枚举 enumerable 接口 - IEnumerable 产生类型 yield type 枚举器 enumerator ...
- JavaScript 作用域和闭包——另一个角度:扩展你对作用域和闭包的认识【翻译+整理】
原文地址 --这篇文章有点意思,可以扩展你对作用域和闭包的认识. 本文内容 背景 作用域 闭包 臭名昭著的循环问题 自调用函数(匿名函数) 其他 我认为,尝试向别人解释 JavaScript 作用域和 ...
- Word模板中的表格处理
在软件系统中,我们经常要输出一些word ,excel,ppt文档,为了输出结果漂亮美观.输出操作方便快捷,通常要制作一些模板文件,通过对模板文件中的关键信息进行修改,就不用管排版.格式等处理了. 在 ...
- VO、DTO与领域模型的概念
业务对象模型(也叫领域模型 domain model)是描述业务用例实现的对象模型.它是对业务角色和业务实体之间应该如何联系和协作以执行业务的一种抽象.业务对象模型从业务角色内部的观点定义了业务用例. ...
- SQL Server临时表
[]SQL Server临时表]()https://docs.microsoft.com/zh-cn/sql/relational-databases/tables/temporal-tables)
- mule学习笔记
mule学习笔记 1.安装&配置 版本:mule 2.2.1 操作: 1) 下载.解压 2)配置环境变量:MULE_HOME.PATH 3)如果网络环境使用代理,找到%MULE_HOME%/c ...
- cocos2d-js 自定义事件监听派发
熟悉js的dom事件或者flash事件的,基本都能立马明白cc.eventManager的用法. cc.eventManager有两种注册监听器的方式,一种是原生事件,例如 cc.eventManag ...