在ListView中重置背景色 [英] Reset background color in a ListView

查看:87
本文介绍了在ListView中重置背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,在其中使用 onListItemClick 事件更改所选项目的背景颜色:

I have a ListView where I'm changing the background color of the selected item using the onListItemClick event:

@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id)
{
    v.setBackgroundColor(Color.GRAY);
}

是否可以在更改单个视图的背景颜色之前将ListView的所有行重置为原始颜色?问题是每个选定的项目都保留其背景色,而我只希望用户单击的最新项目更改.

Is it possible to reset all of the rows of the ListView back to the original color before changing the single view's background color? The problem is that each selected item retains their background color and I only want the latest item the user clicked on changed.

更新:

JRaymond的响应似乎是最好的方法,但是在处理Android布局时这是很常见的,我已经将代码放进去了,什么也没发生.没有办法调试它,没有错误消息,什么也没有.我绝对不喜欢处理Android布局,这是处理UI时最费解,最糟糕的实现设计.

JRaymond's response seems to be the best approach, however, and it's usual when working with Android layouts, I've put the code in and nothing happens. No way to debug it, no error messages, nothing. I absolutely hate dealing with Android layouts, it's the most convoluted, terribly implemented design for dealing with UI.

无论如何,这是我到目前为止所拥有的:

Anyhow, this is what I have so far:

ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"  
    android:layout_height="fill_parent" 
    > 

        <ListView android:id="@android:id/list"
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent" 
            android:choiceMode="singleChoice"
            android:listSelector="@drawable/listing_selector"           
        />

        <TextView android:id="@android:id/empty"
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent" 
            android:padding="5dp"
        />

</LinearLayout>

listing_selector.xml

listing_selector.xml

<?xml version="1.0" encoding="utf-8"?>

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

   <item android:state_focused="true" android:drawable="@android:color/transparent" />
   
   <item android:state_pressed="true" android:drawable="@android:color/transparent" />

   <item android:state_selected="true" android:state_activated="true" android:drawable="@android:color/black" />
   
   <item android:state_activated="true" android:drawable="@android:color/transparent" />    
   
   <item android:state_selected="true" android:drawable="@android:color/transparent" />
   
   <item android:drawable="@android:color/transparent" />
   
</selector>

就像我说的那样,当我单击项目时,什么都没有发生,即使内置的突出显示也消失了.我什至尝试将所有颜色更改为黑色,但是什么也没发生.

Like I said, nothing happens when I click on items, even the built-in highlighting is gone. I even tried changing all of the color to black, but nothing happens.

UPDATE2:

我越来越近了.阅读文章后,您必须将 selector xml放在 res 下的名为 color 的文件夹中,然后将 selector 设置为背景在 ListView 中每个项目的布局中,而不是在 ListView 本身中:

I'm getting closer. After reading this article, you have to place your selector xml in a folder called color under res, then you set the selector as the background in the layout for each item in the ListView, not on the ListView itself:

<?xml version="1.0" encoding="UTF-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"  
    android:layout_height="fill_parent" 
    android:orientation="horizontal"
    android:background="@color/listing_selector"    
>
    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    
</RelativeLayout>

当我触摸 ListView 中的每个项目时,背景颜色都会闪烁,但是它们并没有停留并恢复为原始背景颜色.这是我正在使用的选择器:

I'm seeing the background color change flash when I touch each item in the ListView however, they are not staying and reverting back to the original background color. This is the selector I'm using:

<?xml version="1.0" encoding="utf-8"?>

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

    <item  android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
    
    <item android:state_pressed="true" android:drawable="@color/blue" />
    
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/blue" />

</selector>

color.xml:

color.xml:

<?xml version="1.0" encoding="UTF-8"?>

<resources>    
    <color name="blue">#ff33b5e5</color>        
</resources>

这是一个绝对的痛苦.谁想出了这个糟糕的系统来处理UI,谁都应该被解雇.

What an absolute pain in the ass this is. Whoever came up with this terrible system for dealing with the UI should be fired.

解决方案:

不能100%确定,但这仅适用于Android 3.0+设备.设置背景颜色的是 state_activated .

Not 100% sure, but this may only work in Android 3.0+ devices. state_activated is what sets the background color.

推荐答案

您要查找的是stateListDrawable(也称为选择器)和choiceMode的组合.

What you're looking for is a combination of a stateListDrawable (also known as a selector) and choiceMode.

在您的 drawables 文件夹中包含这样的xml(我使用其他drawable作为背景,但是颜色也一样):

Include in your drawables folder an xml like this one (I'm using other drawables as my backgrounds, but color works just as well):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_focused="true"
          android:drawable="@drawable/list_item_pressed" />
    <item android:state_pressed="true"
          android:drawable="@drawable/list_item_pressed" />
    <item android:state_selected="true"
          android:state_activated="true"
          android:drawable="@drawable/list_item_selected" />
    <item android:state_activated="true"
          android:drawable="@drawable/list_item_selected" />
    <item android:state_selected="true"
          android:drawable="@android:color/black" />
    <item android:drawable="@android:color/transparent" />
</selector>

,然后将列表视图的 choiceMode 设置为 singleChoice

and then set your listview's choiceMode to singleChoice

<ListView
  ...
  android:choiceMode="singleChoice"

这使操作系统可以为您处理背景.

This lets the OS handle your backgrounds for you.

这篇关于在ListView中重置背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆