列出具有交替颜色的项目

时间:2010-01-12 16:35:28

标签: android listview adapter

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

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

但是现在,当我使用滚轮选择项目时,或者当我点击某个项目时,选择/点击的原始颜色不会覆盖我的自定义背景(我可以看到我设置的颜色下方的原始颜色)。 / p>

如何设置这些状态的原始颜色?

2 个答案:

答案 0 :(得分:20)

我认为最简单的方法是创建两个用作背景资源的选择器,在state_selected模式下使用透明颜色: (RES /抽拉/ 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 /抽拉/ 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 /值/ colors.xml:)

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

然后使用setBackgroundResource方法在适配器的getView方法中设置背景:

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

现在,当您选择一行时,您的背景不会隐藏原始选择器。

答案 1 :(得分:2)

您需要更改列表突出显示颜色 如果你通过风格

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

或者您可以在代码中设置相同的属性 my_selector是一个drawable状态 - 查看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>
相关问题