交替的颜色列表项颜色、列表

2023-09-04 07:08:30 作者:逗比味的小仙女

我有一个列表视图和交替设置背景颜色列表项的适配器(斑马线列表样式):

 公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
    INT colorPos =位置%colors.length;
    ...
    convertView.setBackgroundColor(颜色[colorPos]);
    返回convertView;
}
 

但现在,当我使用滚轮选择一个项目,或者当我点击一个项目,原来的颜色选择/点击不重写我的自定义背景(我可以看到下面的一个我原来设定的颜色)。

如何设置原始色彩为这些国家?

解决方案

我觉得最简单的方法是创建用作背景资源,有透明色,在state_selected模式下两个选择: (RES /绘制/ alterselector1.xml:)

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_selected =真
        机器人:可绘制=@可绘制/透明/>
    <项目的android:STATE_ pressed =真
        机器人:可绘制=@可绘制/透明/>
    <项目安卓state_selected =假
        机器人:可绘制=@可绘制/ altercolor1/>

< /选择器>
 

(RES /绘制/ alterselector2.xml:)

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_selected =真
        机器人:可绘制=@可绘制/透明/>
    <项目的android:STATE_ pressed =真
        机器人:可绘制=@可绘制/透明/>
    <项目安卓state_selected =假
        机器人:可绘制=@可绘制/ altercolor2/>
< /选择器>
 
怎么让EXCEL表的行的背景色是两种背景色的交替

(RES /价值/ colors.xml:)

 <资源>
    <绘制NAME =透明>#00FFFFFF< /绘制>
    <绘制NAME =altercolor1>#FFFFFFFF< /绘制>
    <绘制NAME =altercolor2>#FF000000< /绘制>
< /资源>
 

然后,在与setBackgroundResource方法的适配器的getView方法设置的背景

 如果(位置%2 == 0){
    reusableView.setBackgroundResource(R.drawable.alterselector1);
} 其他 {
    reusableView.setBackgroundResource(R.drawable.alterselector2);
}
 

现在,当你选择一排,你的背景不隐藏原来选择的后面。

I have a list view and an adapter that sets alternating background colors to the list items ("zebra" list style):

public View getView(final int position, View convertView, ViewGroup parent) {
    int colorPos = position % colors.length;
    ...
    convertView.setBackgroundColor(colors[colorPos]);
    return convertView;
}

But now, when i select an item using scroll wheel, or when I click an item, the original colors for selecting/clicking do not override my custom backgrounds (I can see the original color below the one I set).

How can I set the original colors for these states?

解决方案

I think the easiest way is to create two selectors which are used as the background resources, with transparent color in the state_selected mode: (res/drawable/alterselector1.xml:)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/transparent" />
    <item android:state_pressed="true"
        android:drawable="@drawable/transparent" />
    <item android:state_selected="false"
        android:drawable="@drawable/altercolor1"/>

</selector>

(res/drawable/alterselector2.xml:)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/transparent" />
    <item android:state_pressed="true"
        android:drawable="@drawable/transparent" />
    <item android:state_selected="false"
        android:drawable="@drawable/altercolor2"/>
</selector>

(res/values/colors.xml:)

<resources>
    <drawable name="transparent">#00ffffff</drawable>
    <drawable name="altercolor1">#ffffffff</drawable>
    <drawable name="altercolor2">#ff000000</drawable>
</resources>

Then you set the backgrounds in the getView method of the adapter with the setBackgroundResource method:

if (position % 2 == 0){
    reusableView.setBackgroundResource(R.drawable.alterselector1);
} else {
    reusableView.setBackgroundResource(R.drawable.alterselector2);
}

Now when you select a row, your background don't hide the original selector behind.