android 自定义ListView中的单项选择

android 自带的 ListView的单项选择item做的很丑,最近写了一个新的 ListView 的单项选择,代码如下:

public ArrayList mArrayListChoosed; private AdapterView.OnItemClickListener listViewItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub ImageView v=(ImageView) view.findViewById(R.id.imageview_bookshelves_radio_button); if (mArrayListChoosed.contains(position)) { mArrayListChoosed.clear(); v.setBackgroundResource(R.drawable.radio_button_off); } else { if (mArrayListChoosed.size() > 0) { View v1 = parent.getChildAt(mArrayListChoosed.get(0)); v1.findViewById(R.id.imageview_bookshelves_radio_button).setBackgroundResource(R.drawable.radio_button_off); mArrayListChoosed.clear(); } mArrayListChoosed.add(position); v.setBackgroundResource(R.drawable.radio_button_on); rowId=id; } } };

 

ListView 中adapter可以是一个基于BaseAdapter的子类,在 getView()中代码如下:

L ayoutInflater inflater; View view; if(convertView == null) { inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.listview_item_manage_bookshelves, null); }else{ view=convertView; } final HolderView holderView = new HolderView(); holderView.mImageViewCheck = (ImageView) view.findViewById(R.id.imageview_bookshelves_check_button); holderView.mTextViewBookName = (TextView) view.findViewById(R.id.textview_manage_book_name); holderView.mTextViewBookAuthorName = (TextView) view.findViewById(R.id.textview_manage_bookshelves_author);

自定义的HolerView 代码如下:

private class HolderView { private ImageView mImageViewCheck; private TextView mTextViewBookName; private TextView mTextViewBookAuthorName; }

 

这样的话,就可以随意定义listview中item的单项选择样式了。

 

你可能感兴趣的:(Android)