前段时间为了实现根据item不同的内容实现不同的背景色google了好久只找到了个隔行换色,通过自定义SimpleAdapter终于实现了此功能,但是定义了selector并没有触发点击效果。今天重新想了一遍终于把这个问题解决了。在自定义的getView里就给每个convertView定义它的背景色和selector。
[代码]自定义SimpleAdapter
01 |
public class MySimpleAdapter extends BaseAdapter { |
02 |
private LayoutInflater mInflater; |
04 |
private List<HashMap<String, Object>> dataList; |
06 |
public MySimpleAdapter(Context context, List<HashMap<String, Object>> data) { |
07 |
mInflater = LayoutInflater.from(context); |
12 |
public int getCount() { |
13 |
return dataList.size(); |
17 |
public Object getItem( int position) { |
22 |
public long getItemId( int position) { |
27 |
public View getView( int position, View convertView, ViewGroup parent) { |
29 |
if (convertView == null ) { |
30 |
convertView = mInflater.inflate(R.layout.color_list_item, null ); |
31 |
holder = new ViewHolder(); |
32 |
holder.blue = (TextView)convertView.findViewById(R.id.blue_item); |
33 |
holder.red = (TextView)convertView.findViewById(R.id.red_item); |
34 |
holder.green = (TextView)convertView.findViewById(R.id.green_item); |
35 |
holder.yellow = (TextView)convertView.findViewById(R.id.yellow_item); |
37 |
convertView.setTag(holder); |
40 |
holder = (ViewHolder)convertView.getTag(); |
43 |
holder.blue.setText((CharSequence)dataList.get(position).get( "blue" )); |
44 |
holder.red.setText((CharSequence)dataList.get(position).get( "red" )); |
45 |
holder.green.setText((CharSequence)dataList.get(position).get( "green" )); |
46 |
holder.yellow.setText((CharSequence)dataList.get(position).get( "yellow" )); |
47 |
if ( "blue" .equals(dataList.get(position).get( "status" ).toString())) { |
48 |
convertView.setBackgroundResource(R.drawable.item_chang_blue); |
49 |
} else if ( "red" .equals(dataList.get(position).get( "status" ).toString())) { |
50 |
convertView.setBackgroundResource(R.drawable.item_chang_red); |
51 |
} else if ( "green" .equals(dataList.get(position).get( "status" ).toString())) { |
52 |
convertView.setBackgroundResource(R.drawable.flightinfo_item_chang_green); |
53 |
} else if ( "yellow" .equals(dataList.get(position).get( "status" ).toString())) { |
54 |
convertView.setBackgroundResource(R.drawable.flightinfo_item_chang_yellow); |
60 |
public void notifyDataSetChanged() { |
61 |
super .notifyDataSetChanged(); |
2. [代码]选择器item_chang_blue.xml 其他同理
01 |
<?xml version= "1.0" encoding= "utf-8" ?> |
03 |
xmlns:android= "http://schemas.android.com/apk/res/android" > |
05 |
<!-- 非触摸模式下获得焦点并单击时的背景图片 --> |
07 |
android:state_focused= "true" |
08 |
android:state_pressed= "true" |
09 |
android:drawable= "@drawable/list_item_pressed" /> |
10 |
<!-- 触摸模式下单击时的背景图片 --> |
13 |
android:state_focused= "false" |
14 |
android:state_pressed= "true" |
15 |
android:drawable= "@drawable/list_item_pressed" /> |
20 |
android:state_selected= "true" |
21 |
android:drawable= "@drawable/list_item_pressed" /> |
24 |
android:state_focused= "true" |
25 |
android:drawable= "@drawable/list_item_pressed" /> |
27 |
<item android:drawable= "@drawable/status_blue" /> |