改变当前ListView项的背景颜色适配器getView方法 [英] Change background colour of current listview item in adapter getView method

查看:756
本文介绍了改变当前ListView项的背景颜色适配器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)用两个选择奇数和偶数现在的位置是项目

These are the following steps to do show. Step1.1) Use two selector for odd and even postion list item

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) artists_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>

第二步) colors.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

<一个href="http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html">http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

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

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