在适配器 getView 方法中更改当前列表视图项的背景颜色 [英] Change background colour of current listview item in adapter getView method

查看:26
本文介绍了在适配器 getView 方法中更改当前列表视图项的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为列表视图构建自定义适配器 - 我希望此适配器为列表视图提供交替的背景颜色.

I am building a custom adapter for a listview - I would like this adapter to give the listview alternating background colours.

boolean alternate = false;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        if (alternate)
        {
            // set background colour of this item?
        }

        alternate = !alternate;

        // lots of other stuff here

        return convertView;     }

如何在上下文中设置列表视图项的背景?

How would I set the background of the listview item in context?

推荐答案

​​

以下是做展示的步骤.Step1.1) 对奇偶位置列表项使用两个选择器

artists_list_backgroundcolor.xml

artists_list_backgroundcolor.xml

<?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="@color/grey" />
<item android:state_pressed="true"
    android:drawable="@color/itemselected" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/itemselected" />
</selector>

步骤 1.2)Artist_list_background_alternate.xml

Step 1.2) artists_list_background_alternate.xml

<?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="@color/sign_out_color" />
<item android:state_pressed="true"
    android:drawable="@color/login_hover" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/login_hover" />
</selector>

步骤 2)颜色.xml

Step2) colors.xml

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

    <color name="survey_toplist_item">#EFEDEC</color>
    <color name="survey_alternate_color">#EBE7E6</color>
    <color name="grey">#ffffff</color>
    <color name="itemselected">#EDEDED</color>
    <color name="login_hover">#E5F5FA</color>
    <color name="sign_out_color">#e84040</color>

</resources>

步骤 3) 在 Arrayadapter 中:

Step 3) In Arrayadapter:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.listitem, parent, false);
        }

        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
        } else {
            view.setBackgroundResource(R.drawable.artists_list_background_alternate);
        }

        ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));

        return view;
    }

欲了解更多详情,请访问 belog 链接

For more details go through belog link

http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

这篇关于在适配器 getView 方法中更改当前列表视图项的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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