1.最简洁的解决办法。
adapter里保存当前选中的位置变量,getView方法里,禁用radiobutton的点击事件,只通过listview的onItemClick事件设置选中位置变量,并且通过notifyDataSetChanged刷新列表。getView方法里当前项位置为选中项时设置radiobutton为选中状态。否则为未选中状态。通过adapter.getSelectedItem方法获取当前选中项数据。
注意:本来想着让阻塞radiobutton的获取焦点、获取点击事件,试了各种设置,最后发现rb_status.setClickable(false);禁用radiobutton的点击事件是有效的。
lv_opt.setAdapter(adapter); lv_opt.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { adapter.setSelectedIndex(position); } });
public class AdapterOptionList extends BaseAdapter<OptionItem> { public AdapterOptionList(Context context, List<OptionItem> list) { super(context, list); } /** <span style="white-space:pre"> </span> * 设置选中的项目 <span style="white-space:pre"> </span> * <span style="white-space:pre"> </span> * @param position <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public void setSelectedIndex(int position) { <span style="white-space:pre"> </span>this.selectedIndex = position; <span style="white-space:pre"> </span>notifyDataSetChanged(); <span style="white-space:pre"> </span>} @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (null == convertView) { viewHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.item_opt,null); viewHolder.rb_status = (RadioButton) convertView .findViewById(R.id.rb_status); viewHolder.tv_title_opt = (TextView) convertView .findViewById(R.id.tv_title_opt); viewHolder.tv_opt_detail = (TextView) convertView .findViewById(R.id.tv_opt_detail); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.rb_status.setClickable(false); OptionItem opt = list.get(position); if(selectedIndex == position){ viewHolder.rb_status.setChecked(true); opt.setSeletedStatus(true); }else{ viewHolder.rb_status.setChecked(false); opt.setSeletedStatus(false); } viewHolder.tv_title_opt.setText(opt.getName()); viewHolder.tv_opt_detail.setText(opt.getDiscription()); return convertView; } class ViewHolder { RadioButton rb_status; TextView tv_title_opt; TextView tv_opt_detail; } }
2.最简单粗暴的办法。
adapter里设置每一个radiobutton的状态集合变量,在radiobutton的setOnCheckedChangeListener方法里设置集合里的每个radiobutton状态,然后通过notifyDataSetChanged刷新列表。
比如http://blog.csdn.net/checkin001/article/details/11519131