我试图在列表中的ListGroup项目中使用这两种颜色:#E1E1E1和#C2C2C2。第一个是偶数项,另一个是奇数项。它的部分'工作。当打开列表的活动打开时,我会得到正确的结果,但仅适用于列表的可见部分。如果我向下滚动,背景都是默认颜色。如果我向下滚动并将手机翻转为横向模式,则会再次创建活动,并且我再次仅为列表的可见部分获得正确的结果。我希望有人可以帮助我一点:)
编辑:
你在图像中看到的是白色的是gray15p。较暗的是gray30p,这是XML中的默认值。机器人:背景=" @颜色/ gray30p&#34 ;.如果我使用,if-else它确实有效,但我不明白为什么仅使用IF就不会应用gray15p(看起来像白色)。
以下是它的外观:
在我的可扩展列表适配器中:
@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>
答案 0 :(得分: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.
}