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;
	}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。