Listview嵌套CheckBox

public class MainActivity extends Activity {

    private ListView lv;
    ArrayList<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
    //listview的Textview准备的文本
    String[] name={"全选","a1","a2","a3","a4","a5","a6"};
    //CheckBox是否点击准备的数组
    Boolean[] bd={false,false,false,false,false,false,false};
    private Abcc abcc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        for (int i = 0; i < name.length; i++) {
            Map<String, Object> m=new HashMap<String,Object>();
            m.put("text", name[i]);
            list.add(m);
        }
        abcc = new Abcc();
        lv.setAdapter(abcc);
        /*SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list, R.layout.listitme, new String[] { "text" },
                new int[] { R.id.tv });
        lv.setAdapter(simpleAdapter);*/
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                CheckBox cd=(CheckBox) view.findViewById(R.id.cb);
                if (cd.isChecked()) {
                    cd.setChecked(false);
                } else {
                    cd.setChecked(true);
                }

                if (position==0&&(cd.isChecked())) {
                    for (int i = 0; i < bd.length; i++) {
                        bd[i]=true;
                    }
                    abcc.notifyDataSetChanged();
                }else if (position == 0 && (!cd.isChecked())) {
                    //如果是取消全选 就把所有的都取消 然后更新
                    for (int i = 0; i < bd.length; i++) {
                        bd[i] = false;
                    }
                    abcc.notifyDataSetChanged();
                }
                if (position != 0 && (!cd.isChecked())){
                    // 如果把其它的选项取消   把全选取消
                    bd[0] = false;
                    bd[position]=false;
                    abcc.notifyDataSetChanged();
                } else if (position != 0 && (cd.isChecked())) {
                    //如果选择其它的选项,看是否全部选择
                    //先把该选项选中 设置为true
                    bd[position]=true;
                    int a = 0;
                    for (int i = 1; i < bd.length; i++) {
                        if (bd[i] == false) {
                            //如果有一个没选中  就不是全选 直接跳出循环
                            break;
                        } else {
                            //计算有多少个选中的
                            a++;
                            if (a == bd.length - 1) {
                                //如果选项都选中,就把全选 选中,然后更新
                                bd[0] = true;
                                abcc.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }
        });
    }
    public class Abcc extends BaseAdapter {


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=convertView.inflate(MainActivity.this, R.layout.listitme, null);
            CheckBox cb=(CheckBox) v.findViewById(R.id.cb);
            TextView tv=(TextView) v.findViewById(R.id.tv);
            String object = (String) list.get(position).get("text");
            tv.setText(object);
            //每次都根据 bd[]来更新checkbox
            if (bd[position]==true) {
                cb.setChecked(true);
            }else if (bd[position]==false) {
                cb.setChecked(false);
            }
            return v;
        }



    }
}

布局文件

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mylistviewcheckbox.MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

android:focusable=”false”
以上必须在CheckBox中设置焦点为false,不然与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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="选择" />

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_alignParentRight="true"
            android:clickable="false"
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>

第二种方法setOnCheckedChangeListener来解决
mainActivity类:

public class MainActivity extends ActionBarActivity {
    ArrayList<ccc> ls=new ArrayList<ccc>();
    private Abc abc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv=(ListView) findViewById(R.id.lv);
        for (int i = 0; i < 20; i++) {
            ls.add(new ccc("A"+i, "b"+i,false));
        }
        abc = new Abc();
        lv.setAdapter(new Abc());
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
    public class Abc extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return ls.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            final ccc ccc;
            if (convertView==null) {
                convertView=convertView.inflate(MainActivity.this, R.layout.aaaaaaa, null);
                vh=new ViewHolder();
                vh.tv=(TextView) convertView.findViewById(R.id.tv);
                vh.cb=(CheckBox) convertView.findViewById(R.id.cb);
                convertView.setTag(vh);
            }else {
                vh=(ViewHolder) convertView.getTag();
            }
             ccc= ls.get(position);
            vh.tv.setText(ccc.getName());
            vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if (isChecked) {
                        ccc.setBl(true);
                    }else {
                        ccc.setBl(false);
                    }
                }
            });
            vh.cb.setChecked(ccc.getBl());
            return convertView;
        }
        class ViewHolder{
            TextView tv;
            CheckBox cb;
        }

    }
}
User用户类:public class ccc {
    String name;
    String ling;
    Boolean bl;
    public ccc(String name,String ling,Boolean bl){
        this.name=name;
        this.ling=ling;
        this.bl=bl;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getLing(){
        return ling;
    }
    public void setLing(String ling){
        this.ling=ling;
    }
    public Boolean getBl(){
        return bl;
    }
    public void setBl(Boolean bl){
        this.bl=bl;
    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="aaaaaaaaa"
        />
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:id="@+id/cb"/>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值