ListView 自身提供了 CheckBox 只需要添加一行代码

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

但是这种实现想要自己控制操作起来局限很多。所以我选择了自己添加CheckBox的方式。可以支持列表项的全选,删除,并保持数据的对应关系不会乱。

列表中的CheckBox选中状态与一个Map进行绑定,利用 adapter.notifyDataSetChanged();来更新界面。

效果如下:

下面直接看代码把。

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#C9F1FF">
  7. <ListView
  8. android:id="@id/android:list"
  9. android:layout_height="wrap_content"
  10. android:layout_width="fill_parent"
  11. android:fadingEdge="none"
  12. android:cacheColorHint="#00000000"/>
  13. <RelativeLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="40.0dip"
  16. android:layout_alignParentBottom="true">
  17. <CheckBox android:id="@+id/all_check_btn"
  18. android:layout_width="40.0dip"
  19. android:background="@drawable/bottom_back_bg"
  20. android:layout_height="40.0dip"
  21. android:layout_alignParentLeft="true"/>
  22. </RelativeLayout>
  23. </RelativeLayout>

item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent" android:layout_height="wrap_content"
  4. android:layout_marginRight="3.0dip" android:layout_weight="1.0"
  5. android:orientation="horizontal" android:descendantFocusability="blocksDescendants">
  6. <CheckBox android:id="@+id/isCheakBox" android:layout_width="wrap_content" android:layout_height="wrap_content"
  7. android:layout_alignParentLeft="true" />
  8. <!-- 日报图片 -->
  9. <ImageView android:id="@+id/dailyPic" android:contentDescription="dailyPic"
  10. android:layout_width="wrap_content" android:layout_height="wrap_content"
  11. android:layout_marginTop="3.0dip" android:src="@drawable/reports"
  12. android:layout_toRightOf="@id/isCheakBox" android:layout_centerVertical="true"/>
  13. <!--附件名称 -->
  14. <TextView
  15. android:id="@+id/dailyName"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_toRightOf="@id/dailyPic"
  19. android:text="日报名称" android:layout_centerVertical="true"
  20. android:textColor="#000000"
  21. android:textSize="12.0sp" />
  22. <ImageButton android:id="@+id/deleteAttachment"
  23. android:layout_width="wrap_content" android:layout_height="wrap_content"
  24. android:layout_marginTop="3.0dip" android:background="@drawable/delete"
  25. android:layout_centerVertical="true" android:focusable="false"
  26. android:layout_alignParentRight="true" android:layout_marginRight="20dp"/>
  27. <!--附件名称 -->
  28. </RelativeLayout>

Activity代码

  1. public class ListViewCheckBoxActivity extends ListActivity {
  2. private static final String TAG = "ListViewCheckBoxActivity";
  3. private List<Item> itemList;
  4. private DraftDailyAdapter adapter;
  5. private Map<Integer, Boolean> isCheckedMap;
  6. private CheckBox allCheckBox;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. allCheckBox = (CheckBox)findViewById(R.id.all_check_btn);
  12. itemList = new ArrayList<Item>();
  13. isCheckedMap = new HashMap<Integer, Boolean>();
  14. //初始化数据
  15. for(int i=0;i<8;i++){
  16. Item item = new Item();
  17. item.id=i;
  18. item.name = "第"+i+"篇日报";
  19. itemList.add(item);
  20. isCheckedMap.put(i,false);
  21. }
  22. adapter = new DraftDailyAdapter(this,itemList);
  23. setListAdapter(adapter);
  24. allCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  25. @Override
  26. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  27. Set<Integer> set = isCheckedMap.keySet();
  28. Iterator<Integer> iterator = set.iterator();
  29. if(isChecked){
  30. while(iterator.hasNext()){
  31. Integer keyId = iterator.next();
  32. isCheckedMap.put(keyId,true);
  33. }
  34. }else{
  35. while(iterator.hasNext()){
  36. Integer keyId = iterator.next();
  37. isCheckedMap.put(keyId,false);
  38. }
  39. }
  40. adapter.notifyDataSetChanged();
  41. }
  42. });
  43. }
  44. class DraftDailyAdapter extends BaseAdapter {
  45. public List<Item> list;
  46. private Context context;
  47. LayoutInflater inflater;
  48. public DraftDailyAdapter(Context context, List<Item> list) {
  49. super();
  50. this.list = list;
  51. this.context = context;
  52. inflater = LayoutInflater.from(this.context);
  53. }
  54. @Override
  55. public int getCount() {
  56. return list == null ? 0 : list.size();
  57. }
  58. @Override
  59. public Object getItem(int location) {
  60. return list.get(location);
  61. }
  62. @Override
  63. public long getItemId(int position) {
  64. return position;
  65. }
  66. @Override
  67. public View getView(int position, View convertView, ViewGroup parent) {
  68. ViewHolder holder = null;
  69. Item item = list.get(position);
  70. //Item的位置
  71. final int listPosition = position;
  72. //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态
  73. final int id = item.id;
  74. if(convertView == null){
  75. holder = new ViewHolder();
  76. convertView = inflater.inflate(R.layout.item, null);
  77. holder.tvName = (TextView)convertView.findViewById(R.id.dailyName);
  78. holder.deleteButton = (ImageButton)convertView.findViewById(R.id.deleteAttachment);
  79. holder.cBox = (CheckBox)convertView.findViewById(R.id.isCheakBox);
  80. convertView.setTag(holder);
  81. }else{
  82. holder = (ViewHolder) convertView.getTag();
  83. }
  84. Log.d(TAG, "id="+id);
  85. holder.cBox.setChecked(isCheckedMap.get(id));
  86. holder.tvName.setText(item.name);
  87. holder.deleteButton.setOnClickListener(new OnClickListener() {
  88. @Override
  89. public void onClick(View paramView) {
  90. //Log.d(TAG, "deletePosition="+listPosition+"");
  91. //删除list中的数据
  92. list.remove(listPosition);
  93. //删除Map中对应选中状态数据
  94. isCheckedMap.remove(id);
  95. //通知列表数据修改
  96. adapter.notifyDataSetChanged();
  97. }
  98. });
  99. holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  100. @Override
  101. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  102. if(isChecked){
  103. isCheckedMap.put(id,true);
  104. }else{
  105. isCheckedMap.put(id,false);
  106. }
  107. }
  108. });
  109. return convertView;
  110. }
  111. public final class ViewHolder {
  112. public TextView tvName;
  113. public ImageButton deleteButton;
  114. public CheckBox cBox;
  115. }
  116. }
  117. class Item {
  118. private Integer id;
  119. private String name;
  120. }
  121. }

资源文件见附件源代码。

安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)的更多相关文章

  1. 我的Android进阶之旅------>Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项

    今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.所以首先我得把系统的语言切换到波兰语,问题是哪个是波兰语呢? 我还真的不认识哪个列表项代表着 ...

  2. 实现listview中checkbox的多选与记录

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  3. android UI进阶之实现listview中checkbox的多选与记录

    今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...

  4. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  5. android ListView中CheckBox错位的解决

    貌似已经非常晚了,可是还是想记下笔记,想让今天完满. 在ListView中加了checkBox,但是发现点击改变其选中状态的时候,发现其位置错乱.状态改变的并非你选中的,百思不得其解.后面通过上网查资 ...

  6. ListView中CheckBox使用问题

    因为CheckBox的点击事件优先级比ListView的高,所以当ListView中使用CheckBox会导致ListView的setOnItemClickListener失去响应. 解决的方法:在C ...

  7. 抛砖引玉:关于Android的ListView中CheckBox错乱

    首先:参考了这篇翻译的文章:http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html 文章中关于说的Android中的Recyc ...

  8. Android 解决listview中checkBox错位选择

    假如ListView,分成2页(或者设置数据可以纵向拉,可隐藏),每页3条数据,每个Listview的Item 里面有个checkBox,现在,当我选择第一页的前两天数据,翻到第二页,竟然第二页后两条 ...

  9. ListView中CheckBox错乱解决

    思路: ListView在复用的时候会出现很多问题,CheckBox状态会出现错乱,解决思路: 1.使用Map集合的键值对的形式来存放position位置上CheckBox的状态 2.监听CheckB ...

随机推荐

  1. Java-Shiro(二):HelloWord

    新建项目&&配置pom.xml导入包 新建maven java project项目: 修改pom.xml: <project xmlns="http://maven.a ...

  2. Javascript 闭包(Closures)

    本文内容 闭包 闭包和引用 参考资料 闭包是 JavaScript 的重要特性,非常强大,可用于执行复杂的计算,可并不容易理解,尤其是对之前从事面向对象编程的人来说,对 JavaScript 认识和编 ...

  3. AngularJs - Javascript MVC 框架

    在2012年6月google发布了AngularJs 1.0稳定版, 并宣称:AngularJS可以让你扩展HTML的语法,以便清晰.简洁地表示应用程序中的组件,并允许将标准的HTML作为你的模板语言 ...

  4. Discuz常见小问题-如何为每个板块设置不同的图标

    进入后台的论坛-版块管理,选中要修改图标的板块,点击后面的编辑 在板块图标中找到图标文件,一般是PNG或者GIF,大小为32X32,提交之后效果如下

  5. 微信小程序 - scroll-into-view(提示)

    scroll-view的参数scroll-into-view适用于索引以及回到顶部 .详情见官方文档scroll-view: 点击下载:scroll-into-view示例

  6. 微信小程序 - 上传图片组件

    2019-01-08 更新至1.1:修复了一些问题 2019-03-14 全面更新,推荐:https://www.cnblogs.com/cisum/p/10533559.html 使用了es8的as ...

  7. 使用PyInstaller打包Python角本为exe程序

    一.经过测试 在Windows平台请使用Windows平台的pyinstaller,Linux平台请使用Linux平台的Pyinstall角本. 二.命令如下: pyinstaller -F --ic ...

  8. Centos7中修改Hostname的方法

    一.Centos7中修改的方法: hostnamectl set-hostname <new hostname> 说明:centOS 7 里面修改hostname的方式有所改变,修改/et ...

  9. 方法(method)和函数(function)有什么区别?

    方法(method)和函数(function)有什么区别? 定义和参数区别 函数是独立的功能,与对象无关,需要显示的传递数据 方法与对象和类相关,依赖对象而调用,可以直接处理对象上的数据,也就是隐式传 ...

  10. java.net.ConnectException: Connection refused: no further information

    NIO项目中出现了这个错误: java.net.ConnectException: Connection refused: no further information 一般是因为InetSocket ...