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

android ListView单选功能

时间:2014-12-11 17:34:02      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:des   android   blog   io   ar   os   sp   on   2014   

在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单选功能

标签:des   android   blog   io   ar   os   sp   on   2014   

原文地址:http://blog.csdn.net/u010152805/article/details/41867867

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