2
我已經擴展了SimpleAdapter來在ListView中設置交替顏色。我可以確認getView正在被調用。但是,屏幕上的顏色變化永遠不會發生。我已經嘗試過android:cacheColorHint =「#00000000」,但這不起作用。有任何想法嗎?交替Android ListView顏色不能正常工作
<?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="wrap_content">
<Spinner android:id="@+id/summary_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:prompt="@string/summary_spinner_prompt"/>
<Button android:id="@+id/summary_button_show"
android:text="@string/summary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5sp"
android:layout_toRightOf="@id/summary_spinner"/>
<TextView android:id="@+id/summary_list_header1"
android:text="@string/summary_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/summary_spinner"
android:background="#8B8989"
android:textColor="#000000"/>
<TextView android:id="@+id/summary_list_header2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/summary_spinner"
android:layout_alignParentRight="true"
android:background="#8B8989"
android:textColor="#000000"/>
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/summary_list_header1"
android:cacheColorHint="#00000000"
android:visibility="visible"/>
</RelativeLayout>
public class MySimpleAdapter extends SimpleAdapter
{
private int[] colors = new int[] { 0xEEE9E9, 0xCDC9C9 };
public MySimpleAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to)
{
super(context, items, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}