[英]ListView item background color
Today I have faced a strange problem in implementing an adapter for ListView. 今天,在实现ListView适配器时遇到了一个奇怪的问题。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = (View) inflater.inflate(R.layout.test_list_item, parent , false);
}else{
rowView = (ViewGroup) convertView;
}
Log.d(String.valueOf(position), String.valueOf(testList.get(position).progress));
if (testList.get(position).progress == 0){
rowView.setBackgroundColor(Color.parseColor("#BDBDBD"));
}
return rowView;
}
And here is the LogCat output. 这是LogCat的输出。
tag msg
D/0 : 100
D/1 : 90
D/2 : 80
D/3 : 70
D/4 : 0
D/5 : 0
D/6 : 0
D/0 : 100
D/1 : 90
D/2 : 80
D/3 : 70
D/4 : 0
D/5 : 0
D/6 : 0
As you can see for all indexes besides 0 , 1 , 2 , 3 the background color should change. 如您所见,除了0,1,2,3以外的所有索引,背景颜色都应该改变。 But when the the activity starts all the rows change their background color besides 2-nd , 3-rd , 4-th rows (the first one changes too though it's progress is grater than 0).
但是,当活动开始时,除了第二行,第三行,第四行外,所有行都会更改其背景颜色(第一个行也会更改,尽管它的进度比0大)。 Moreover when I scroll all the way down and up again all the rows appear to change their background color including rows number 2 ,3 ,4.
此外,当我再次上下滚动时,所有行似乎都改变了背景色,包括行号2,3,4。 How can I make the rows only with progress equal to 0 change their color ?
我如何才能使行仅等于0的行更改其颜色?
add an else
statement to the color change 在颜色变化中添加
else
语句
if (testList.get(position).progress == 0){
rowView.setBackgroundColor(Color.parseColor("#BDBDBD"));
} else {
rowView.setBackgroundColor(Color.parseColor("#Color of the other cells"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.