【问题标题】:List items with alternating colors列出交替颜色的项目
【发布时间】:2010-01-12 16:35:28
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android listview adapter


    【解决方案1】:

    我认为最简单的方法是创建两个选择器用作背景资源,在 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);
    }
    

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

    【讨论】:

    • 这部分有效 - 当使用滚动按钮聚焦项目时,我可以看到突出显示,但按下项目时我无法使其工作。我尝试了此处列出的所有状态:developer.android.com/guide/topics/resources/…,但没有任何效果...
    • 我也编辑了选择器来处理按下状态。似乎当您按下该项目时,它会失去其选定状态。因此,您也必须将按下状态定义为透明。只需注意排序,因为选择器将使用与当前状态匹配的第一个项目,因此 state_selected="false" 项目应该在底部。
    • 有效!我问这个问题快一年了!!谢谢!
    【解决方案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>
    

    【讨论】:

    • 我试过了,但是我一开始为背景设置的任何内容都会被代码中创建的“斑马”所覆盖...是否有另一种方法来创建斑马外观?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2012-06-10
    • 2014-03-31
    相关资源
    最近更新 更多