1

我正在尝试使用 2 种颜色、白色和灰色交错颜色的 ListView。在我的 getView 方法的适配器中,我执行以下代码:

if(position % 2 == 0){
    v.setBackgroundColor(Color.WHITE);
}else{
    v.setBackgroundResource(R.color.light_grey_listas);
}
 return v;

但在我的屏幕中,有时一组线条颜色相同。例如,3 行具有灰色背景色,或者当我在列表视图中导航时,行更改为错误的颜色

4

2 回答 2

2

在此处输入图像描述

这些是下面要做的步骤。

Step1.1) 对奇偶位置列表项使用两个选择器

艺术家列表背景色.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/grey" />
<item android:state_pressed="true"
    android:drawable="@color/itemselected" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/itemselected" />
</selector>

步骤 1.2) Artists_list_background_alternate.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/sign_out_color" />
<item android:state_pressed="true"
    android:drawable="@color/login_hover" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/login_hover" />
</selector>

Step2) 颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="survey_toplist_item">#EFEDEC</color>
    <color name="survey_alternate_color">#EBE7E6</color>
    <color name="grey">#ffffff</color>
    <color name="itemselected">#EDEDED</color>
    <color name="login_hover">#E5F5FA</color>
    <color name="sign_out_color">#e84040</color>

</resources>



@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.listitem, parent, false);
        }

        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
        } else {
            view.setBackgroundResource(R.drawable.artists_list_background_alternate);
        }

        ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));

        return view;
    }
}

您可以在下面的链接中获得完整的描述

http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

于 2013-09-17T12:29:27.763 回答
1

试试这个 getView() 方法:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
ViewHolder holder;
        if (view == null) {
            holder=new ViewHolder();
            view = lInflater.inflate(R.layout.listitem, parent, false);
            view.setTag(hoder);

        }else{
            holder = (ViewHolder) view.getTag();
        }

        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
        } else {
            view.setBackgroundResource(R.drawable.artists_list_background_alternate);
        }

        ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));

        return view;
    }

 class ViewHolder {
        //Declare here your listview variables      }
于 2013-09-17T12:45:31.633 回答