ListView中的项目设定颜色 [英] Setting color of ListView items

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

问题描述

我想单独设置如电话号码和电子邮件地址蓝某​​的ListView项目的颜色,让用户知道他们点击。这里有一个片段,但实施这一根本不能很好地工作,当你滚动列表列表项会随机改变颜色。

 列表与LT;地图<字符串,字符串>>数据=新的ArrayList<地图<字符串,字符串>>();    //...add数据列出    SimpleAdapter适配器=新SimpleAdapter(这一点,数据,R.layout.simple_list_item_2_custom,新的String [] {LABEL,VALUE},
        新的INT [] {R.id.text1,R.id.text2})
    {
        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            观点的LinearLayout =(的LinearLayout)super.getView(位置,convertView,父母);            字符串标签= data.get(位置)获得(LABEL);            如果(label.equals(电子邮件地址))
            {
                ((TextView的)view.getChildAt(0))setTextColor(Color.BLUE);
            }            返回视图。
        }
    };

而这里的定义列表项中的LinearLayout:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    安卓了minHeight =机器人:ATTR /列表preferredItemHeight
    机器人:方向=垂直>
    < TextView的机器人:ID =@ + ID / text1中
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginStart =机器人:ATTR /列表preferredItemPaddingStart
        机器人:layout_marginTop =8DIP
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?
    < TextView的机器人:ID =@ + ID /文本2
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignStart =@机器人:ID / text1中
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceListItem?
< / LinearLayout中>


解决方案

您错过了别人,如果(label.equals(电子邮件地址))

您必须放置另一个文本颜色时,它不是一个电子邮件地址,如下所示:
((的TextView)view.getChildAt(0))setTextColor(Color.WHITE);

原因是引起的Andr​​oid适配器重用其没有长正在显示的旧线,在这种情况下,已定义到蓝色所述一个

I'm trying to individually set the color of certain ListView items like phone number and email address to blue, so that user knows they're clickable. Here's a snippet but implementing this doesn't work well at all, list items will randomly change colors when you scroll the list.

    List<Map<String, String>> data = new ArrayList<Map<String, String>>();

    //...add data to list

    SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.simple_list_item_2_custom, new String[] { LABEL, VALUE },
        new int[] { R.id.text1, R.id.text2 })
    {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            LinearLayout view = (LinearLayout)super.getView(position, convertView, parent);

            String label = data.get(position).get(LABEL);

            if (label.equals("Email Address"))
            {
                ((TextView)view.getChildAt(0)).setTextColor(Color.BLUE);
            }

            return view;
        }
    };

And here's the LinearLayout that defines a list item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="vertical" >
    <TextView android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
        android:layout_marginTop="8dip"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    <TextView android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@android:id/text1"
        android:textAppearance="?android:attr/textAppearanceListItem" />
</LinearLayout>

解决方案

You missed the else of "if (label.equals("Email Address"))"

You have to place another text color when it is not an email address, like this: ((TextView)view.getChildAt(0)).setTextColor(Color.WHITE);

The reason is cause Android adapter reuses the old line which is no long being displayed, in this case the one you already defined to blue.

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

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