7

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

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

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

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

4

2 回答 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>

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

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

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

于 2010-11-26T15:28:38.730 回答
2

如果您通过样式进行更改,则需要更改列表突出显示颜色

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

或者您可以在代码中设置相同的属性 my_selector 是可绘制的状态 - 在 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>
于 2010-01-12T16:55:36.490 回答