ListView里的CheckBox使用

这里写图片描述

先说布局
布局也就一个listview + 一个button,然后item的布局如下

<?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">

<LinearLayout
    android:id="@+id/ll"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_name"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/gray_33"
        android:layout_marginRight="10dp"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/tv_description"
        android:layout_gravity="center_vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textSize="14sp"
        android:layout_height="wrap_content"/>

    <CheckBox
        android:id="@+id/item_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/gray_33"
        android:layout_gravity="center_vertical" />
</LinearLayout>

</LinearLayout>

lv_data.setAdapter(new ServiceAdapter(Main.this,initData()));

ServiceAdapter

public class ServiceAdapter extends BaseAdapter {

    private List<ServiceItem.ServicesBean> mDataset;
    private Context context;
    public LinkedHashMap<Integer, ServiceItem.ServicesBean> cbMap = new LinkedHashMap<>();
    // 用来控制CheckBox的选中状况
    private LinkedHashMap<Integer, Boolean> isSelected;
    private StringBuilder sb = new StringBuilder();

    public ServiceAdapter(Context context, List<ServiceItem.ServicesBean> mDataset) {
        this.mDataset = mDataset;
        this.context = context;
        isSelected = new LinkedHashMap<>();
        initDate();
    }

    private void initDate() {
        for (int i = 0; i < mDataset.size(); i++) {
            isSelected.put(i, false);
        }
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        final int index = position;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_service, null);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.cb.setChecked(isSelected.get(position));
        viewHolder.tv_name.setText(mDataset.get(position).getName());
        viewHolder.tv_description.setText(mDataset.get(position).getIntroduce());

        viewHolder.cb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (isSelected.get(position)) {
                    isSelected.put(position, false);
                    cbMap.remove(position);
                } else {
                    isSelected.put(position, true);
                    cbMap.put(position, mDataset.get(position));
                }

                if(cbMap.size() > 4){
                    viewHolder.cb.toggle();
                    ToastUtils.showToast((Activity)context,"最多选择四个");
                    cbMap.remove(position);
                    isSelected.put(position, false);
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        public CheckBox cb;
        public TextView tv_name;
        public TextView tv_description;
    }

    //供activity获取选中的数据
    public List<ServiceItem.ServicesBean> getSelectedData(){
        if(cbMap.size() > 0){
            List<ServiceItem.ServicesBean> mSelectedData = new ArrayList<>();
            for (Integer key : cbMap.keySet()) {
                mSelectedData.add(mDataset.get(key));
            }
            return mSelectedData;
        }else{
            return null;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值