在ListView中更改项目的颜色-Android [英] Changing color of items in ListView - Android

查看:67
本文介绍了在ListView中更改项目的颜色-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,我想更改列表中所选项目的颜色.我可以在下面更改所选项目的颜色,但是如何将所有其他项目的颜色恢复(或更改)为原始颜色?

I have a ListView and I would like to change the color of the selected item in the list. I can change the color of the selected item below, but how do I revert (or change) the color of all the other items back to their original color?

@Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        v.setBackgroundColor(Color.CYAN);
    }

我尝试更改父项的颜色,但未更改项目的颜色:

I tried changing the color of the parent but it doesn't change the color of the items:

 @Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        parent.setBackgroundColor(Color.WHITE);
        v.setBackgroundColor(Color.CYAN);
    }

ListView:

        <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:drawSelectorOnTop="false" />

每个项目是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout android:id="@+id/linear"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text=""/>

    <TextView
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/sipExt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#59696E"
        android:gravity="right"
        android:layout_marginRight="9dp"
        android:textStyle="italic"
        android:text=""/>

</LinearLayout>

    <TextView
    android:id="@+id/alias"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="#a69d86"
    android:textStyle="italic"
    android:layout_below="@id/linear"
    android:text=""/>

推荐答案

执行此操作的正确方法是定义自定义选择器并设置颜色(可绘制)并根据需要设置颜色他们处于每个状态.有关详细信息,请参见此链接:

There proper way to do this is to define a custom Selector and set the color (Drawable) and set the colors as you want them to be in each state. See this link for details:

无法将自定义选择器添加到ListView

在这里:

Android ListView所选项目保持突出显示

-关于SO的其他文章也很多.

-- there are plenty of other posts on SO regarding this as well.

如果您想保留当前的设计,可以尝试如下操作:

If you want to keep your current design you can try something like this:

 View lastTouchedView;

 @Override
 public void onListItemClick(ListView parent, View v, int position, long id) 
    lastTouchedView.setBackgroundColor(Color.WHITE);
    v.setBackgroundColor(Color.CYAN);
    lastTouchedView = v;
 }

这篇关于在ListView中更改项目的颜色-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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