交替使用Android ListView颜色无法正常工作

时间:2011-02-26 14:36:01

标签: java android-widget

我扩展了SimpleAdapter以在ListView中设置交替颜色。我可以确认getView被调用了。但是,颜色变化从不会在屏幕上出现问题。我到处尝试了android:cacheColorHint =“#00000000”,但这不起作用。有什么想法吗?

<?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;  

    } 

}

1 个答案:

答案 0 :(得分:1)

看起来你正在设置alpha = 0的颜色;尝试添加alpha组件(例如,而不是0xEE9E9E使用0xFFEE9E9E)。你正在设置完全透明的颜色......