<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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_weight="1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反 选"
android:layout_weight="1"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全不选"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:id="@+id/listveiw"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
listveiw.xml
如果ListView的Item中包含CheckBox,由于CheckBox的焦点优先级高于ListView,所以当选中ListView时,ListView将不会有选中的状态,解决的方法就是在checkbox组件中加一属性:android:focusable="false"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ddd"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
MainActivity.java
package com.bawei.day04_listview;
import java.util.ArrayList;
import java.util.List;
import com.bawei.vo.Good;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private ListView listView;
private List<Good> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
listView = (ListView) findViewById(R.id.listveiw);
list = new ArrayList<Good>();
for (int i = 0; i < 60; i++) {
list.add(new Good("PP"+i, false));
}
BaseAdapter adapter=new Adper(list,MainActivity.this,button1,button2,button3);
listView.setAdapter(adapter);
}
}
list的vo包Good.java
package com.bawei.vo;
public class Good {
private String name;
private boolean bo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getBo() {
return bo;
}
public void setBo(boolean bo) {
this.bo = bo;
}
@Override
public String toString() {
return "Good [name=" + name + ", bo=" + bo + "]";
}
public Good(String name, boolean bo) {
super();
this.name = name;
this.bo = bo;
}
public Good() {
super();
}
}
Adper.java
package com.bawei.day04_listview;
import java.util.List;
import com.bawei.vo.Good;
import android.content.Context;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class Adper extends BaseAdapter {
List<Good> list;
Context context;
Button button1;
Button button2;
Button button3;
public Adper(List<Good> list, Context context, Button button1, Button button2, Button button3) {
// TODO Auto-generated constructor stub
this.list=list;
this.context=context;
this.button1=button1;
this.button2=button2;
this.button3=button3;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if(convertView==null){
convertView=View.inflate(context, R.layout.listveiw, null);
viewHolder=new ViewHolder();
viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position).getName());
//checkbox监听
viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
list.get(position).setBo(isChecked);
}else{
list.get(position).setBo(isChecked);
}
}
});
//全选
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
//改变boolean
list.get(i).setBo(true);
//刷新
notifyDataSetChanged();
}
}
});
//反选
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getBo()){
list.get(i).setBo(false);
}else{
list.get(i).setBo(true);
}
notifyDataSetChanged();
}
}
});
//全不选
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
list.get(i).setBo(false);
notifyDataSetChanged();
}
}
});
//显示checkBox
viewHolder.checkBox.setChecked(list.get(position).getBo());
return convertView;
}
class ViewHolder{
TextView textView;
CheckBox checkBox;
}
}