简体   繁体   English

扩展列表:ListGroup项目的不同背景颜色

[英]Expandable List: different background colors for ListGroup Items

I'm trying to have these two colors for the ListGroup items in my list: #E1E1E1 and #C2C2C2. 我正在尝试为列表中的ListGroup项使用这两种颜色:#E1E1E1和#C2C2C2。 The first for the even items and the other for the odd ones. 第一个用于偶数项,另一个用于奇数项。 It's 'partially' working. 这是“部分”工作。 When the activity that opens the list is opened, I get the correct result but ONLY FOR THE VISIBLE PART of the list. 当打开打开列表的活动时,我得到正确的结果,但仅适用于列表的可见部分 If I scroll down, the backgrounds are all of the default color. 如果我向下滚动,则背景均为默认颜色。 If I scroll down and flip my phone into landscape mode, the activity gets created again and, again, I get the correct result only for the visible part of the list. 如果向下滚动并将手机翻转为横向模式,则将再次创建活动,并且仅对于列表的可见部分,我才能获得正确的结果。 I hope someone can help me a bit :) 我希望有人可以帮我一下:)

EDIT: 编辑:

what you see as white in the image is gray15p. 您在图像中看到的白色是gray15p。 The darker is gray30p which is the default in the XML. 较深的是gray30p,这是XML中的默认值。 android:background="@color/gray30p". 机器人:背景= “@颜色/ gray30p”。 If I use, if-else it does work, but I can't understand why the gray15p (looks like white) is not being applied using the IF alone. 如果使用if-else,它确实可以工作,但是我不明白为什么单独使用IF不能应用gray15p(看起来像白色)。

Here's how it looks: 外观如下:

名单

In my expandable List Adapter: 在我的可扩展列表适配器中:

@Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }


        ...
        ...

        //set background color in odd items
        if(groupPosition%2==0){
            LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout);
            groupLayout.setBackgroundResource(R.color.gray15p);
        }

        return convertView;

and the layout file list_group.xml 和布局文件list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/groupLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray30p"
    android:orientation="horizontal" >
    <!--  android:padding="8dp"
    android:background="#000000"> -->

    <ImageView
        android:id="@+id/lblListHeaderImage"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingTop="1dp"
        android:paddingBottom="1dp"
        android:src="@drawable/defaultimg"
        android:scaleType="fitCenter"
        android:contentDescription="@string/app_name"/>


    <!--level 1 of expandable list -->
    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="wrap_content"
        android:layout_height="90dp"
        android:paddingLeft="10dp"
        android:textSize="22sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:gravity="center"
        android:textColor="#000000" />

</LinearLayout>

I think whats missing is to set the color for the even items. 我认为缺少的是为偶数项目设置颜色。 The Problem is that you adapter loads the layout with the white background and changes it for the odd items to grey, but when you scroll down, the views gets reused. 问题是适配器将白色背景加载到布局中,并将奇数项更改为灰色,但是当您向下滚动时,视图将被重用。 What happen is that your views background is already set to grey. 发生的情况是您的视图背景已经设置为灰色。

just adjust your code to something like that 只需将代码调整为类似的内容

    //set background color in odd items
    LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout)
    if(groupPosition%2==0){
        groupLayout.setBackgroundResource(R.color.gray15p);
    } else {
        groupLayout.setBackgroundResource(R.color.gray30p); // <-- important part.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM