2016-07-26 89 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)) 
}