Android ListView单选CheckBox

来源:互联网 发布:苹果手机游戏推荐 知乎 编辑:程序博客网 时间:2024/05/29 04:56

思路
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();        }    }
//ListViewListView 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”保证不会覆盖下方的按钮

0 0