listview0.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/myRadioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="无" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选模式" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选模式" /> </RadioGroup> <ListView android:id="@+id/mylistview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:divider="@android:color/darker_gray" android:dividerHeight="1dp" android:fadingEdge="none" android:scrollingCache="false" > </ListView> </LinearLayout>
list_item_0.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_margin="5dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/img" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="22sp" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="13sp" /> </LinearLayout> </RelativeLayout>
list_item_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/child_list_select_item" android:state_pressed="true"/> <item android:drawable="@drawable/child_list_bg_1" android:state_activated="true"/> <item android:drawable="@drawable/child_list_item"/> </selector>
使用了 android:state_activated属性,API至少是11,否则部分效果无法显示,本人机器就是那样!
My0Adapter.java
package com.wangzhu.demoselector; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class My0Adapter extends BaseAdapter { private Context context; private LayoutInflater inflater; private List<Map<String, Object>> items; public My0Adapter(Context context, List<Map<String, Object>> items) { this.context = context; this.items = items; init(); } private void init() { inflater = LayoutInflater.from(context); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (null == convertView) { convertView = inflater.inflate(R.layout.list_item_0, null); viewHolder = new ViewHolder(); viewHolder.img = (ImageView) convertView.findViewById(R.id.img); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.info = (TextView) convertView.findViewById(R.id.info); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.img.setBackgroundResource(Integer.parseInt(String .valueOf(items.get(position).get("img")))); viewHolder.title.setText(String.valueOf(items.get(position) .get("title"))); viewHolder.info .setText(String.valueOf(items.get(position).get("info"))); convertView.setBackgroundResource(R.drawable.list_item_bg_selector); return convertView; } }
ViewHolder.java
package com.wangzhu.demoselector; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public final class ViewHolder { public ImageView img; public TextView title; public TextView info; public CheckBox checkBox; }
ListView0Activity.java
package com.wangzhu.demoselector; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.RadioGroup; public class ListView0Activity extends Activity { private final static String TAG = "ListView0Activity"; private My0Adapter adapter; private ListView listView; private RadioGroup myRadioGroup; private List<Map<String, Object>> datas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview0); init(); } private void init() { getDatas(); adapter = new My0Adapter(this, datas); listView = (ListView) findViewById(R.id.mylistview); listView.setAdapter(adapter); myRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup); myRadioGroup .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case 1: listView.setChoiceMode(ListView.CHOICE_MODE_NONE); break; case 2: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); break; case 3: listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); break; default: break; } // 自定义的BaseAdapter中调用notifyDataSetChanged方法会重新调用BaseAdapter的getView方法 adapter.notifyDataSetChanged(); } }); myRadioGroup.check(1); } public void getDatas() { datas = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; for (int i = 0; i < 20; i++) { map = new HashMap<String, Object>(); map.put("title", "G" + i); map.put("info", "Google " + i); map.put("img", R.drawable.ic_launcher); datas.add(map); } } }
备注:欢迎各位大牛多多指教,谢谢!