Android 中ListView带复选框多选、全选、不选处理

工作快一年了,最近用到ListView带复选框功能,虽然已经做过多次了,但对一些初学者还是有一定的困难,在这里顺便总结一下,供大家参考!!!同时希望大家提出意见!!!!

废话不多说,先看效果

效果一:点击全选,所有复选框选中; 或每个item分别点击选中会触发全选框选中;

Android 中ListView带复选框多选、全选、不选处理

效果二:效果一的状态下,随意取消一个item选中状态,此时全选框改变为不选中

 

Android 中ListView带复选框多选、全选、不选处理

效果三:点击全选框为不选中状态,此时所有item为不选中状态

Android 中ListView带复选框多选、全选、不选处理

下面是代码部分:

一、首先是activity_main.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout

  3. xmlns:android="http://schemas.android.com/apk/res/android"

  4. xmlns:tools="http://schemas.android.com/tools"

  5. android:layout_width="match_parent"

  6. android:layout_height="match_parent"

  7. android:orientation="vertical"

  8. tools:context="csttech.com.checkdemo.MainActivity">

  9.  
  10. <CheckBox

  11. android:id="@+id/ckb_main"

  12. android:layout_width="match_parent"

  13. android:layout_height="wrap_content"

  14. android:background="#666666"

  15. android:padding="10dp"

  16. android:text="全选"/>

  17.  
  18. <ListView

  19. android:id="@+id/list_main"

  20. android:layout_width="match_parent"

  21. android:layout_height="match_parent">

  22.  
  23. </ListView>

  24. </LinearLayout>

二、item.xml格式

 

 

 
  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. <LinearLayout

  8. android:layout_width="match_parent"

  9. android:layout_height="wrap_content"

  10. android:gravity="center_vertical"

  11. android:orientation="horizontal"

  12. android:padding="10dp">

  13.  
  14. <TextView

  15. android:id="@+id/text_title"

  16. android:layout_width="wrap_content"

  17. android:layout_height="wrap_content"

  18. android:text="1"/>

  19.  
  20. <CheckBox

  21. android:id="@+id/ckb"

  22. android:focusable="false"

  23. android:focusableInTouchMode="false"

  24. android:clickable="false"

  25. android:layout_width="wrap_content"

  26. android:layout_height="wrap_content"/>

  27.  
  28. </LinearLayout>

  29. </LinearLayout>


三、MainActivity.class

 

 

 
  1. package csttech.com.checkdemo;

  2.  
  3. import android.os.Bundle;

  4. import android.support.v7.app.AppCompatActivity;

  5. import android.util.Log;

  6. import android.widget.CheckBox;

  7. import android.widget.CompoundButton;

  8. import android.widget.ListView;

  9.  
  10. import java.util.ArrayList;

  11. import java.util.List;

  12.  
  13. public class MainActivity extends AppCompatActivity {

  14. private ListView mListView;

  15. private List<Model> models;

  16. private CheckBox mMainCkb;

  17. private MyAdapter mMyAdapter;

  18. //监听来源

  19. public boolean mIsFromItem = false;

  20.  
  21.  
  22. @Override

  23. protected void onCreate(Bundle savedInstanceState) {

  24. super.onCreate(savedInstanceState);

  25. setContentView(R.layout.activity_main);

  26. initView();

  27. initData();

  28. initViewOper();

  29. }

  30.  
  31. /**

  32. * view初始化

  33. */

  34. private void initView() {

  35. mListView = (ListView) findViewById(R.id.list_main);

  36. mMainCkb = (CheckBox) findViewById(R.id.ckb_main);

  37. }

  38.  
  39. /**

  40. * 数据加载

  41. */

  42. private void initData() {

  43. //模拟数据

  44. models = new ArrayList<>();

  45. Model model;

  46. for (int i = 0; i < 15; i++) {

  47. model = new Model();

  48. model.setSt(i + "******");

  49. model.setIscheck(false);

  50. models.add(model);

  51. }

  52. }

  53.  
  54. /**

  55. * 数据绑定

  56. */

  57. private void initViewOper() {

  58. mMyAdapter = new MyAdapter(models, this, new AllCheckListener() {

  59.  
  60. @Override

  61. public void onCheckedChanged(boolean b) {

  62. //根据不同的情况对maincheckbox做处理

  63. if (!b && !mMainCkb.isChecked()) {

  64. return;

  65. } else if (!b && mMainCkb.isChecked()) {

  66. mIsFromItem = true;

  67. mMainCkb.setChecked(false);

  68. } else if (b) {

  69. mIsFromItem = true;

  70. mMainCkb.setChecked(true);

  71. }

  72. }

  73. });

  74. mListView.setAdapter(mMyAdapter);

  75. //全选的点击监听

  76. mMainCkb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

  77. @Override

  78. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

  79. //当监听来源为点击item改变maincbk状态时不在监听改变,防止死循环

  80. if (mIsFromItem) {

  81. mIsFromItem = false;

  82. Log.e("mainCheckBox", "此时我不可以触发");

  83. return;

  84. }

  85.  
  86. //改变数据

  87. for (Model model : models) {

  88. model.setIscheck(b);

  89. }

  90. //刷新listview

  91. mMyAdapter.notifyDataSetChanged();

  92. }

  93. });

  94.  
  95. }

  96. //对item导致maincheckbox改变做监听

  97. interface AllCheckListener {

  98. void onCheckedChanged(boolean b);

  99. }

  100. }


四、MyAdapter.class

 

 

 
  1. package csttech.com.checkdemo;

  2.  
  3. import android.content.Context;

  4. import android.util.Log;

  5. import android.view.LayoutInflater;

  6. import android.view.View;

  7. import android.view.ViewGroup;

  8. import android.widget.BaseAdapter;

  9. import android.widget.CheckBox;

  10. import android.widget.TextView;

  11.  
  12. import java.util.List;

  13.  
  14. /**

  15. * Created by zhaiydong on 2017/1/9.

  16. */

  17. public class MyAdapter extends BaseAdapter {

  18. private List<Model> data;

  19. private Context context;

  20. private MainActivity.AllCheckListener allCheckListener;

  21.  
  22. public MyAdapter(List<Model> data, Context context, MainActivity.AllCheckListener allCheckListener) {

  23. this.data = data;

  24. this.context = context;

  25. this.allCheckListener = allCheckListener;

  26. }

  27.  
  28. @Override

  29. public int getCount() {

  30. return data.size();

  31. }

  32.  
  33. @Override

  34. public Object getItem(int i) {

  35. return data.get(i);

  36. }

  37.  
  38. @Override

  39. public long getItemId(int i) {

  40. return i;

  41. }

  42.  
  43. @Override

  44. public View getView(final int i, View view, ViewGroup viewGroup) {

  45. ViewHoder hd;

  46. if (view == null) {

  47. hd = new ViewHoder();

  48. LayoutInflater layoutInflater = LayoutInflater.from(context);

  49. view = layoutInflater.inflate(R.layout.item_list, null);

  50. hd.textView = (TextView) view.findViewById(R.id.text_title);

  51. hd.checkBox = (CheckBox) view.findViewById(R.id.ckb);

  52. view.setTag(hd);

  53. }

  54. Model mModel = data.get(i);

  55. hd = (ViewHoder) view.getTag();

  56. hd.textView.setText(mModel.getSt());

  57.  
  58. Log.e("myadapter", mModel.getSt() + "------" + mModel.ischeck());

  59. final ViewHoder hdFinal = hd;

  60. hd.checkBox.setChecked(mModel.ischeck());

  61. view.setOnClickListener(new View.OnClickListener() {

  62. @Override

  63. public void onClick(View view) {

  64. CheckBox checkBox = hdFinal.checkBox;

  65. if (checkBox.isChecked()) {

  66. checkBox.setChecked(false);

  67. data.get(i).setIscheck(false);

  68. } else {

  69. checkBox.setChecked(true);

  70. data.get(i).setIscheck(true);

  71. }

  72. //监听每个item,若所有checkbox都为选中状态则更改main的全选checkbox状态

  73. for (Model model : data) {

  74. if (!model.ischeck()) {

  75. allCheckListener.onCheckedChanged(false);

  76. return;

  77. }

  78. }

  79. allCheckListener.onCheckedChanged(true);

  80.  
  81.  
  82. }

  83. });

  84.  
  85.  
  86. return view;

  87. }

  88.  
  89. class ViewHoder {

  90. TextView textView;

  91. CheckBox checkBox;

  92. }

  93.  
  94. }


五、Model

 

 

 
  1. package csttech.com.checkdemo;

  2.  
  3. /**

  4. * Created by zhaiydong on 2017/1/9.

  5. */

  6. public class Model {

  7. private boolean ischeck;

  8. private String st;

  9.  
  10. public boolean ischeck() {

  11. return ischeck;

  12. }

  13.  
  14. public void setIscheck(boolean ischeck) {

  15. this.ischeck = ischeck;

  16. }

  17.  
  18. public String getSt() {

  19. return st;

  20. }

  21.  
  22. public void setSt(String st) {

  23. this.st = st;

  24. }

  25. }

demo下载地址:http://download.****.net/detail/android_koukou/9733420