最近用到了listView列表的单选功能,网上查了好多多数是用readioButton来实现,但是水平太次居然没看懂。我找啊找 最后终于找到一个个人感觉挺简单的
首先是布局 很简单 就是一个listView 但是里面要有choiceMode这句话 ,表示此listView只能单选
<ListView android:id="@+id/select_dispatch_unit_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="singleChoice">ListView>
下面是listView的Adapter
public class SelectDispatchUnitAdapter extends BaseAdapter { private Context context; private Listdatas; private Handler handler; private int checked = -1;//初始选择为-1,position没有-1嘛,那就是谁都不选咯 public SelectDispatchUnitAdapter(Context context, List datas, Handler handler) { this.context = context; this.datas = datas; this.handler = handler; } public void setChecked(int checked) {//设定一个选中的标志位,在activity中传入值。 this.checked = checked; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parentView) { final ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_select_dispatch_unit_list_item, null); holder = new ViewHolder(); holder.selectDispatchUnitCheck = (ImageView) convertView.findViewById(R.id.select_dispatch_unit_check); holder.selectDispatchUnitNum = (TextView) convertView.findViewById(R.id.select_dispatch_unit_num); holder.selectDispatchUnitName = (TextView) convertView.findViewById(R.id.select_dispatch_unit_name); holder.selectDispatchUnitLayout = convertView.findViewById(R.id.select_dispatch_unit_layout); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.selectDispatchUnitNum.setText(String.valueOf(position + 1)); holder.selectDispatchUnitName.setText(datas.get(position).getUnitName()); if (checked == position) {//如果点击的项目正好是position这一项
/*将前面的小圆设置为选中状的图标*/ holder.selectDispatchUnitCheck.setImageResource(R.mipmap.list_ic_danxuankuang_xuanzhong); } else {
/*将前面的小圆设置为未选中状的图标*/
holder.
selectDispatchUnitCheck.setImageResource(R.mipmap.
list_ic_danxuankuang_moren); }
return convertView; }
class ViewHolder { ImageView
selectDispatchUnitCheck; TextView
selectDispatchUnitNum; TextView
selectDispatchUnitName; View
selectDispatchUnitLayout; }}
上面adapter里的setCheck()方法有什么用呢?别着急 下面在activity就可以用到
selectDispatchUnitLv = (ListView) findViewById(R.id.select_dispatch_unit_lv);
/*我这里传handler有用 你们可以忽略*/ final SelectDispatchUnitAdapter adapter = new SelectDispatchUnitAdapter(this, datas, mHandler); selectDispatchUnitLv.setAdapter(adapter); selectDispatchUnitLv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { adapter.setChecked(position);//传入现在选择的position 现在用到了 adapter.notifyDataSetInvalidated();//重绘 } });
效果图
子布局
<LinearLayout android:id="@+id/select_dispatch_unit_layout" android:layout_width="match_parent" android:layout_height="35dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="5dp"> <ImageView android:id="@+id/select_dispatch_unit_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:button="@null" android:src="@mipmap/list_ic_danxuankuang_moren" /> <TextView android:id="@+id/select_dispatch_unit_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="1" android:textColor="@color/bg_btn_blue_color" android:textSize="16sp" /> <TextView android:id="@+id/select_dispatch_unit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:singleLine="true" android:text="单位名字" android:textColor="@color/bg_btn_blue_color" android:textSize="16sp"/> LinearLayout>