2014-04-04 57 views
0

我想让列表中的ListGroup项目具有以下两种颜色:#E1E1E1和#C2C2C2。第一个是偶数项目,第二个是奇数项目。这是'部分'的工作。当打开列表的活动打开时,我会得到正确的结果,但仅适用于列表的可见部分。如果向下滚动,背景全部是默认颜色。如果向下滚动并将手机翻转为横向模式,则会再次创建该活动,并且再次仅为列表的可见部分获取正确的结果。我希望有人能帮助我一点:)可扩展列表:ListGroup项目的不同背景颜色

编辑:

你所看到的白色的图像是gray15p。较暗的是gray30p,这是XML中的默认设置。机器人:背景= “@颜色/ gray30p”。如果我使用,如果 - 否则它可以工作,但我不明白为什么grey15p(看起来像白色)没有单独使用IF来应用。

下面是它的外观:

list

在我的扩展列表适配器:

@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; 

和布局文件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> 

回答

1

我认为什么是失踪设置偶数项目的颜色。问题在于你的适配器加载了白色背景的布局,并将其中的奇数项目变为灰色,但是当你向下滚动时,视图被重用。发生什么事是您的视图背景已经设置为灰色。

只是调整您的代码以类似的东西

//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. 
    } 
+0

你所看到的白色的图像是gray15p。较暗的是gray30p,这是XML中的默认设置。机器人:背景= “@颜色/ gray30p”。如果我使用,如果 - 否则它可以工作,但我不明白为什么grey15p(看起来像白色)没有单独应用于IF。 – fersarr

+0

比使用'R.color.gray30p'白色。但是这应该可以解决问题。 :) –

+0

是的,我明白你说什么,它确实工作:)但仍然无法理解你的意思是'意见得到重用'或为什么它不能单独与IF一起工作。 grey30p在默认情况下在XML中设置 – fersarr