转载请注明出处:http://blog.youkuaiyun.com/android_koukou/article/details/54290010
最近用到ListView带复选框功能,虽然已经做过多次了,但对一些初学者还是有一定的困难,在这里顺便总结一下,供大家参考!!!同时希望大家提出意见!!!!
废话不多说,先看效果
效果一:点击全选,所有复选框选中; 或每个item分别点击选中会触发全选框选中;
效果二:效果一的状态下,随意取消一个item选中状态,此时全选框改变为不选中
效果三:点击全选框为不选中状态,此时所有item为不选中状态
下面是代码部分:
一、首先是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="csttech.com.checkdemo.MainActivity">
<CheckBox
android:id="@+id/ckb_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#666666"
android:padding="10dp"
android:text="全选"/>
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
二、item.xml格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<CheckBox
android:id="@+id/ckb"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
三、MainActivity.class
package csttech.com.checkdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private List<Model> models;
private CheckBox mMainCkb;
private MyAdapter mMyAdapter;
//监听来源
public boolean mIsFromItem = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initViewOper();
}
/**
* view初始化
*/
private void initView() {
mListView = (ListView) findViewById(R.id.list_main);
mMainCkb = (CheckBox) findViewById(R.id.ckb_main);
}
/**
* 数据加载
*/
private void initData() {
//模拟数据
models = new ArrayList<>();
Model model;
for (int i = 0; i < 15; i++) {
model = new Model();
model.setSt(i + "******");
model.setIscheck(false);
models.add(model);
}
}
/**
* 数据绑定
*/
private void initViewOper() {
mMyAdapter = new MyAdapter(models, this, new AllCheckListener() {
@Override
public void onCheckedChanged(boolean b) {
//根据不同的情况对maincheckbox做处理
if (!b && !mMainCkb.isChecked()) {
return;
} else if (!b && mMainCkb.isChecked()) {
mIsFromItem = true;
mMainCkb.setChecked(false);
} else if (b) {
mIsFromItem = true;
mMainCkb.setChecked(true);
}
}
});
mListView.setAdapter(mMyAdapter);
//全选的点击监听
mMainCkb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//当监听来源为点击item改变maincbk状态时不在监听改变,防止死循环
if (mIsFromItem) {
mIsFromItem = false;
Log.e("mainCheckBox", "此时我不可以触发");
return;
}
//改变数据
for (Model model : models) {
model.setIscheck(b);
}
//刷新listview
mMyAdapter.notifyDataSetChanged();
}
});
}
//对item导致maincheckbox改变做监听
interface AllCheckListener {
void onCheckedChanged(boolean b);
}
}
四、MyAdapter.class
package csttech.com.checkdemo;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.List;
/**
* Created by zhaiydong on 2017/1/9.
*/
public class MyAdapter extends BaseAdapter {
private List<Model> data;
private Context context;
private MainActivity.AllCheckListener allCheckListener;
public MyAdapter(List<Model> data, Context context, MainActivity.AllCheckListener allCheckListener) {
this.data = data;
this.context = context;
this.allCheckListener = allCheckListener;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHoder hd;
if (view == null) {
hd = new ViewHoder();
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.item_list, null);
hd.textView = (TextView) view.findViewById(R.id.text_title);
hd.checkBox = (CheckBox) view.findViewById(R.id.ckb);
view.setTag(hd);
}
Model mModel = data.get(i);
hd = (ViewHoder) view.getTag();
hd.textView.setText(mModel.getSt());
Log.e("myadapter", mModel.getSt() + "------" + mModel.ischeck());
final ViewHoder hdFinal = hd;
hd.checkBox.setChecked(mModel.ischeck());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox = hdFinal.checkBox;
if (checkBox.isChecked()) {
checkBox.setChecked(false);
data.get(i).setIscheck(false);
} else {
checkBox.setChecked(true);
data.get(i).setIscheck(true);
}
//监听每个item,若所有checkbox都为选中状态则更改main的全选checkbox状态
for (Model model : data) {
if (!model.ischeck()) {
allCheckListener.onCheckedChanged(false);
return;
}
}
allCheckListener.onCheckedChanged(true);
}
});
return view;
}
class ViewHoder {
TextView textView;
CheckBox checkBox;
}
}
五、Model
package csttech.com.checkdemo;
/**
* Created by zhaiydong on 2017/1/9.
*/
public class Model {
private boolean ischeck;
private String st;
public boolean ischeck() {
return ischeck;
}
public void setIscheck(boolean ischeck) {
this.ischeck = ischeck;
}
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
}