Listview的全选反选非常常用,今天总结了下:
activity_main
<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" >
<TextView
android:id="@+id/selectnum"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/selectall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />
<Button
android:id="@+id/reverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消选择" />
<Button
android:id="@+id/delect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item
<?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:layout_alignParentLeft="true"/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
MainActivity.class
import java.util.ArrayList;
import java.util.List;
import com.example.auser.checkboxselectlx.Adapter.Holder;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button selectall;
private Button reverse;//反选
private Button cancel;//取消
private Button delect;//删除
private ListView listview;
private Adapter adapter;
private List<String> listStr;
private TextView selectnum;
private int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listStr=new ArrayList<String>();
listAdd();
selectall=(Button) findViewById(R.id.selectall);
reverse=(Button) findViewById(R.id.reverse);
cancel=(Button) findViewById(R.id.cancel);
delect=(Button) findViewById(R.id.delect);
listview=(ListView) findViewById(R.id.list);
selectnum=(TextView)findViewById(R.id.selectnum);
adapter=new Adapter(MainActivity.this,listStr);
listview.setAdapter(adapter);
selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < listStr.size(); i++) {
adapter.isMap().put(i,true);
}
num=listStr.size();
dataNew();
}
});
reverse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < listStr.size(); i++) {
if (adapter.isMap().get(i)) {
adapter.isMap().put(i,false);
num--;
}else{
adapter.isMap().put(i,true);
num++;
}
}
dataNew();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < listStr.size(); i++) {
if (adapter.isMap().get(i)) {
adapter.isMap().put(i,false);
num--;
}
}
dataNew();
}
});
delect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<String> tempList=new ArrayList<String>();
for (int i = 0; i < listStr.size(); i++) {
if (adapter.isMap().get(i)) {
adapter.isMap().put(i,false);
tempList.add(listStr.get(i));
num=0;
}
}
listStr.removeAll(tempList);
dataNew();
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Holder holder=(Holder) arg1.getTag();
holder.check.toggle();//改变状态
Adapter.isMap().put(arg2, holder.check.isChecked());
if (holder.check.isChecked()==true) {
num++;
}else{
num--;
}
selectnum.setText("已选择"+num+"项");
}
});
}
public void listAdd(){
for (int i = 0; i < 20; i++) {
listStr.add("str"+i);
}
}
public void dataNew(){
adapter.notifyDataSetChanged();
selectnum.setText("已选择"+num+"项");
}
}
Adapter.class
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Adapter extends BaseAdapter {
private Context context;
private List<String> list;
private static Map<Integer,Boolean> isMap;
private int type;
public Adapter(Context context,List<String> list) {
this.context=context;
this.list=list;
isMap=new HashMap<Integer, Boolean>();
for (int i = 0; i < list.size(); i++) {
isMap().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView==null) {
holder=new Holder();
convertView=LinearLayout.inflate(context, R.layout.item, null);
holder.text=(TextView) convertView.findViewById(R.id.text);
holder.check=(CheckBox) convertView.findViewById(R.id.check);
convertView.setTag(holder);
}else{
holder=(Holder) convertView.getTag();
}
holder.text.setText(list.get(position));
holder.check.setChecked(isMap().get(position));
return convertView;
}
class Holder{
TextView text;
CheckBox check;
}
public static Map<Integer, Boolean> isMap(){
return isMap;
}
}