文章目录
实现单选必须要设置ListView为CHOICE_MODE_SINGLE(listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)或android:choiceMode=”singleChoice”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| private int cur_pos = 0; private String[] items_text = { "选项一", "选项二", "选项三", "选项四", "选项五" }; ... final Mydapter dapter = new Mydapter(this); ListView listview = (ListView) findViewById(R.id.listView); listview.setAdapter(dapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { cur_pos = position; dapter.notifyDataSetChanged(); } });
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Mydapter extends BaseAdapter { private LayoutInflater inflater;
public Mydapter(Context context) { inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); }
@Override public int getCount() { return items_text.length; }
@Override public Object getItem(int position) { return items_text[position]; }
@Override public long getItemId(int position) { return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { Log.e("TEST", "refresh once"); convertView = inflater.inflate(R.layout.item, null, false); TextView tv = (TextView) convertView.findViewById(R.id.tv); tv.setText(items_text[position]); if (position == cur_pos) { convertView.setBackgroundColor(R.drawable.channel_list_item_bg_selected); } return convertView; } }
|
例子