首页 > 代码库 > 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单选功能