ListView嵌套CheckBox控制选择数量,错乱优化

    我们经常经行的选择条目的操作,就是运用这个ListView加CheckBox实现的,本文简单实现一个效果


直接上代码

Activity部分

private ListView lv;
//放内容的集合
private ArrayList<String>list = new ArrayList<>();
//记录选中条目
private int num = 0;
//用来控制选中
private Map<Integer,Boolean> map = new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.list_view);
    //初始化集合
    for(int i = 0; i<5 ;i++){
        list.add("恶意挂机");
        list.add("故意送人头");
        list.add("不积极参团");
        list.add("言语辱骂");
    }
    //添加适配器
    MyAdapter adapter = new MyAdapter();
    lv.setAdapter(adapter);

}

class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if(convertView == null){
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.my_adapter,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        vh.checkBox.setText(list.get(position));
        //这里写checkBox点击事件,因为ListView条目的点击事件一般和这个有两种用途
        vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                //是否选中
                if(isChecked){
                    num++;
                    map.put(position,isChecked);
                }else{
                    num--;
                    map.remove(position);
                }
                //这里只能选择三个条目
                if(num > 3){
                    //控制checkBox为不可选中
                    vh.checkBox.setChecked(false);
                    //选中条目减一
                    num--;
                    Toast.makeText(MainActivity.this,"只能选择三个",Toast.LENGTH_SHORT).show();

                }
            }
        });

        //条目错乱
        if(map!=null&&map.containsKey(position)){
            vh.checkBox.setChecked(true);
        }else {
            vh.checkBox.setChecked(false);
        }

        return convertView;
    }

    class ViewHolder{
        private CheckBox checkBox;
        public ViewHolder(View v){
            checkBox = v.findViewById(R.id.checkbox);
        }
    }
}

Activity的Xml文件

只有一个ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

</LinearLayout>

Adapter的Xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值