标签:
//--------------主布局文件----------------------------
<RelativeLayout 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"
>
<ListView android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linear"
android:id="@+id/list_view"></ListView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linear"
android:layout_alignParentBottom="true">
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:id="@+id/all"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选"
android:id="@+id/reverse"/>
</LinearLayout>
</RelativeLayout>
//--------------listview 条目的布局--------------------
<?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="horizontal" >
<CheckBox
android:id="@+id/ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
//------------MainActivity中-----------------------
package com.bwie.test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView list_view;
private CheckBox allBox;
private Button reverse;
private CheckAdapter checkAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
list_view = (ListView) findViewById(R.id.list_view);
allBox = (CheckBox) findViewById(R.id.all);
reverse = (Button) findViewById(R.id.reverse);
//初始化数据
List<String> list=new ArrayList<String>();
for (int i = 0; i < 15; i++) {
list.add("这是条目"+i);
}
//设置适配器
checkAdapter = new CheckAdapter(list, this);
list_view.setAdapter(checkAdapter);
//设置全选
allBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//记录选中状态
boolean flag=allBox.isChecked();
//遍历,把全选框的状态给每个条目
for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
checkAdapter.getSelect().set(i, flag);
}
//刷新适配器
checkAdapter.notifyDataSetChanged();
}
});
//设置反选
reverse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果不包含选中的,也就是条目一个也没选中,就吐司,否则反选
if (!checkAdapter.getSelect().contains(true)) {
Toast.makeText(MainActivity.this, "请选择", 0).show();
}else{
//遍历,把选中的设置未选中
for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
if (checkAdapter.getSelect().get(i)) {
checkAdapter.getSelect().set(i, false);
}else{
//把为选中的设置为选中
checkAdapter.getSelect().set(i, true);
}
}
//判断如果包含未选中的,全选框设置为未选中
if (checkAdapter.getSelect().contains(false)) {
allBox.setChecked(false);
}
//刷新适配器
checkAdapter.notifyDataSetChanged();
}
}
});
}
//适配器 用的内部类
private class CheckAdapter extends BaseAdapter{
private List<String> list;
private Context context;
//创建一个集合 去记录选中与未选中的状态
LinkedList<Boolean> linkedList=new LinkedList<Boolean>();
public CheckAdapter(List<String> list, Context context) {
super();
this.list = list;
this.context = context;
//设置默认值为不选中
for (int i = 0; i < list.size(); i++) {
linkedList.add(false);
}
}
//对外提供一个方法 获取这个集合
private List<Boolean> getSelect(){
return linkedList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=View.inflate(context, R.layout.item, null);
}
CheckBox ck=(CheckBox) convertView.findViewById(R.id.ck);
TextView tView=(TextView) convertView.findViewById(R.id.tv);
tView.setText(list.get(position));
ck.setChecked(linkedList.get(position));
//对checkBox点击进行监听
ck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点中的时候改成相反的值
linkedList.set(position, !linkedList.get(position));
//点击的同时还要去判断全选 包含false
if (linkedList.contains(false)) {
allBox.setChecked(false);
}else{
allBox.setChecked(true);
}
notifyDataSetChanged();
}
});
return convertView;
}
}
}
标签:
原文地址:http://www.cnblogs.com/changyiqiang/p/5936629.html