2016-09-29 86 views
0

我已经在可扩展列表视图中设置了备用组标题的两种颜色。但是,当我多次点击展开或折叠颜色变化到任何组行。可扩展列表视图组标题颜色变化

这里是我的代码,

if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 

     if(groupPosition % 2 == 1) { 
      convertView.setBackgroundColor(Color.parseColor("#3C3C3C")); 
     }else { 
      convertView.setBackgroundColor(Color.parseColor("#000000")); 
     } 
    } 

这是滚动列表后发生。我也试过这一个

private int[] colors = new int[] { Color.parseColor("#000000"), Color.parseColor("#3C3C3C") }; 
int colorPos = groupPosition % colors.length; 
convertView.setBackgroundColor(colors[colorPos]); 

回答

1

你可以试试这个:

if (groupPosition % 2 == 0) 
{ 
    convertView.setBackgroundColor(Color.parseColor("#3C3C3C")); 
} 
else 
{ 
    convertView.setBackgroundColor(Color.parseColor("#000000")); 
} 
+0

我已经尝试this.but不工作。颜色发生变化,但是在颜色转换后,颜色会移动到任何一行。 –

+0

首先检查:if(convertView == null){ \t \t \t convertView = inflater.inflate(R.layout.listrow_group,null); \t \t}然后在If条件的旁边设置代码。 –

+0

在我的代码::如果(convertView == NULL){ LayoutInflater infalInflater =(LayoutInflater)this._context \t \t \t \t \t .getSystemService(Context.LAYOUT_INFLATER_SERVICE); \t \t \t convertView = infalInflater.inflate(R.layout.list_item,null); \t \t} –

0

做的

if(groupPosition % 2 == 0) 

代替

if(groupPosition % 2 == 1) 

并保持如果境外convertView的= =空检查

if (convertView == null) { 
    LayoutInflater infalInflater = (LayoutInflater) this._context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = infalInflater.inflate(R.layout.list_group, null); 
} 

if(groupPosition % 2 == 0) { 
     convertView.setBackgroundColor(Color.parseColor("#3C3C3C")); 
    }else { 
     convertView.setBackgroundColor(Color.parseColor("#000000")); 
    } 
1
if(groupPosition % 2 == 0) { 
      convertView.setBackgroundColor(Color.parseColor("#3c3c3c")); 
     }else { 
      convertView.setBackgroundColor(Color.parseColor("#000000")); 
     } 

把这个代码,如果(convertView == NULL)超出此条件

+0

谢谢Mohit,它的工作。 –

+0

如果您满意,请接受答案 –