ListView+CheckBox实现全选 单击效果
在网上也找了一些案例,但都是用Map来实现的.我的是把对象绑定到当前控件上.代码稍微简洁.
main布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <LinearLayout android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_below="@+id/tv" android:orientation="horizontal" > <Button android:id="@+id/bt_selectall" android:layout_width="80dp" android:layout_height="fill_parent" android:text="全选" /> <Button android:id="@+id/bt_deselectall" android:layout_width="80dp" android:layout_height="fill_parent" android:text="取消选择" /> </LinearLayout> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/line" /> </RelativeLayout>
ListItem布局文件:listviewitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/item_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" /> <CheckBox android:id="@+id/item_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:gravity="center_vertical" /> </LinearLayout>
实体类:Person.java
package com.example.listview_checkbox; public class Person { private int id; private String name; private boolean selected; public Person(int id, String name, boolean selected) { super(); this.id = id; this.name = name; this.selected = selected; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }
MainActivity.java
package com.example.listview_checkbox; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity{ private List<Person> listPerson; private List<Person> selectPerson=new ArrayList<Person>(); private MyAdapter mAdapter; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listView=(ListView) findViewById(R.id.lv); listPerson = new ArrayList<Person>(); initDate(); listView.setAdapter(mAdapter=new MyAdapter()); Button btSelectall=(Button) findViewById(R.id.bt_selectall); btSelectall.setOnClickListener(clickListener); Button btDeselectall=(Button) findViewById(R.id.bt_deselectall); btDeselectall.setOnClickListener(clickListener); tv=(TextView) findViewById(R.id.tv); } // 初始化数据 private void initDate() { for (int i = 0; i < 15; i++) { Person person=new Person(i, "复选框"+i, false); listPerson.add(person); } } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return listPerson.size(); } @Override public Object getItem(int position) { return listPerson.get(position); } @Override public long getItemId(int position) { return listPerson.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { // 获得ViewHolder对象 holder = new ViewHolder(); // 导入布局并赋值给convertview convertView=View.inflate(MainActivity.this, R.layout.listviewitem, null); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); // 为view设置标签 convertView.setTag(holder); } else { // 取出holder holder = (ViewHolder) convertView.getTag(); } Person person=listPerson.get(position); // 设置list中TextView的显示 holder.tv.setText(person.getName()); // 根据isSelected来设置checkbox的选中状况 holder.cb.setChecked(person.isSelected()); holder.cb.setTag(person); holder.cb.setOnClickListener(clickListener); return convertView; } public class ViewHolder { TextView tv; CheckBox cb; } } private OnClickListener clickListener=new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.item_cb: Person person = (Person) v.getTag(); person.setSelected(!person.isSelected()); if (person.isSelected()) { selectPerson.add(person); }else { selectPerson.remove(person); } tv.setText("您选中了"+selectPerson.size()+"条数据!!"); break; case R.id.bt_selectall: selected(true); break; case R.id.bt_deselectall: selected(false); break; default: break; } } }; private void selected(boolean selected){//选择或者取消选中 selectPerson.clear(); for(int i=0;i<listPerson.size();i++){ Person person=listPerson.get(i); person.setSelected(selected); if(selected==true){ selectPerson.add(person); } } mAdapter.notifyDataSetChanged(); tv.setText("您选中了"+selectPerson.size()+"条数据!!"); } }
效果图如下: