码迷,mamicode.com
首页 > 移动开发 > 详细

Android ListView单选CheckBox

时间:2016-05-07 07:50:41      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

思路
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”保证不会覆盖下方的按钮

Android ListView单选CheckBox

标签:

原文地址:http://blog.csdn.net/mwh613245/article/details/51332801

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!