Android ListView单选CheckBox

思路
1触发事件setOnItemClickListener改变CheckBox
2在适配器的getView()里改变CheckBox状态,需要记录选中的CheckBox位置
3适配器提供方法改变CheckBox
4用notifyDataSetChanged启动getView()
实现代码

//适配器
public class MyAdapter extends BaseAdapter {
        // 填充数据的list
        private ArrayList<Map<String, Object>> list;
        // 用来控制CheckBox的选中状况
        private HashMap<Integer,Boolean> isSelected;
        // 上下文
        private Context context;
        // 用来导入布局
        private LayoutInflater inflater = null;

        // 构造器
        public MyAdapter(ArrayList<Map<String, Object>> list, Context context) {
            this.context = context;
            this.list = list;
            inflater = LayoutInflater.from(context);
            isSelected = new HashMap<Integer, Boolean>();
            // 初始化数据
            initDate();
        }

        // 初始化isSelected的数据
        private void initDate(){
            for(int i=0; i<list.size();i++) {
                getIsSelected().put(i,false);
            }
        }

        @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(int position, View convertView, ViewGroup parent) {

            // 导入布局并赋值给convertview
            convertView = inflater.inflate(R.layout.item_exchange_self, null);
            TextView tv_name = (TextView) convertView.findViewById(R.id.exchange_self_name);
            TextView tv_address = (TextView) convertView.findViewById(R.id.exchange_self_address);
            TextView tv_date = (TextView) convertView.findViewById(R.id.exchange_self_date);
            CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.select_address);
            // 设置list中TextView的显示
            tv_name.setText(list.get(position).get("name").toString());
            tv_address.setText(list.get(position).get("address").toString());
            tv_date.setText(list.get(position).get("workDates").toString());
            // 根据isSelected来设置checkbox的选中状况
            checkBox.setChecked(getIsSelected().get(position));
            return convertView;
        }

        public HashMap<Integer,Boolean> getIsSelected() {
            return isSelected;
        }

        public void setIsSelected(int postiton) {
            initDate();
            this.isSelected.put(postiton, true);
            notifyDataSetChanged();
        }
    }
//ListView
ListView exchangeCenters = (ListView) findViewById(R.id.exchange_centers);
        final MyAdapter myadapter = new MyAdapter(getData(),this);
        exchangeCenters.setAdapter(myadapter);
        exchangeCenters.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                myadapter.setIsSelected(position);
                exchangeCenter = getData().get(position);
            }
        });

布局的注意事项
1.CheckBox自身的点击要关闭,添加属性android:clickable=”false”
2.item的总部局需要加android:descendantFocusability=”blocksDescendants”保证ListView点击事件正常
3.ListView属性加android:layout_weight=”1”保证不会覆盖下方的按钮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>