交替显示列表项颜色

7

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

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

但是现在,当我使用滚轮选择一个项目或者点击一个项目时,原本用于选择/点击的颜色不会覆盖我的自定义背景颜色(我可以看到我设置的颜色下面的原始颜色)。
如何设置这些状态的原始颜色?
2个回答

20

我认为最简单的方法是创建两个选择器作为背景资源,在state_selected模式下使用透明颜色:

(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>

然后您可以使用适配器的getView方法和setBackgroundResource方法来设置背景:

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

现在当你选择一行时,背景不会遮盖在原来的选择器后面了。


这部分有效 - 当使用滚动按钮聚焦到项目时,我可以看到高亮显示,但当项目被按下时它无法正常工作。我尝试了这里列出的所有状态:http://developer.android.com/guide/topics/resources/color-list-resource.html,但没有任何效果... - zorglub76
1
我已经编辑了选择器,以处理按下状态。当你按下项目时,它似乎失去了它的选定状态。因此,您必须将按下状态定义为透明。只需注意排序,因为选择器将使用与当前状态匹配的第一个项目,所以state_selected="false"的项目应该在底部。 - Utyi
可以了!距离我提出这个问题已经快一年了!!谢谢! - zorglub76

2

如果您希望通过样式更改列表的高亮颜色,则需要进行以下操作:

 <style name="Widget.AbsListView">
        <item name="android:listSelector">@drawable/my_selector</item>
 </style>

或者你可以在代码中设置相同的属性。 我的选择器是一个状态可绘制对象 - 在SDK目录中寻找示例:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_window_focused="false"
        android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_disabled" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />

    <item android:state_focused="true"
        android:drawable="@drawable/list_selector_background_focus" />

</selector>

我试过了,但是无论我最初为背景设置什么,都会被在代码中创建“斑马线”覆盖...还有其他方法可以创建斑马线外观吗? - zorglub76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接