2016-07-26 78 views
0

寻找解决方案。我需要根据文本更改项目颜色。 下面是我的来自ExpandableListAdapter的片段。它工作正常,但是当我上下滚动时,所有项目都具有相同的颜色。任何解决方案?ExpandableListView项目颜色

@Override 
public View getChildView(int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 


final ListItem childText = (ListItem) getChild(groupPosition, childPosition); 

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

    final TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem); 

    txtListChild.setText(childText.getName()); 
    txtListChild.setTag(childText.getDocumentGuid()); 

    txtListChild.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

       if (!txtListChild.getTag().equals("HEADER")) { 
        Toast.makeText(getApplicationContext(), "" + txtListChild.getTag(), Toast.LENGTH_LONG).show(); 

       } else { 
        //header// 

      } 
     } 
    }); 

    if(childText.getDocumentGuid().equals("HEADER")) { 
     txtListChild.setTextColor(context.getResources().getColor(R.color.color_accent)); 
    } 

    return convertView; 
    } 

回答

0

首先,纠正我,如果我错了,ListViewRecyclerView前身也“回收”是移过屏幕视图。因此,您应该添加的一件事是您的if的子句else

if(childText.getDocumentGuid().equals("HEADER")) { 
    txtListChild.setTextColor(context.getResources().getColor(R.color.color_accent)); 
} else { 
txtListChild.setTextColor(context.getResources().getColor(R.color.color_something_else)) 
}