首页 > 代码库 > android ListView单选功能
android ListView单选功能
在Adapter getView内部实现控制。
private int selectPosition =-1; class PayAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return payList==null?0:payList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return payList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.pay_listview_item_layout, null); holder = new ViewHolder(); holder.image = (ImageView) convertView.findViewById(R.id.pay_bank_icon_imageview); holder.title = (TextView) convertView.findViewById(R.id.pay_bank_title_textview); holder.description = (TextView) convertView.findViewById(R.id.pay_bank_description_textview); holder.radio = (RadioButton) convertView.findViewById(R.id.pay_select_radiobtn); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } Pay pay = (Pay) getItem(position); holder.image.setImageResource(getResources().getIdentifier(pay.getImage(), "drawable", context.getPackageName())); holder.title.setText(pay.getTitle()); holder.description.setText(pay.getDescription()); holder.radio.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ selectPosition = position; } notifyDataSetChanged(); } }); if(selectPosition==position){ holder.radio.setChecked(true); }else{ holder.radio.setChecked(false); } if(position==0){ holder.radio.setChecked(true); } return convertView; } } class ViewHolder{ ImageView image; TextView title; TextView description; RadioButton radio; }
android ListView单选功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。