繁体   English   中英

ListView项目背景色

[英]ListView item background color

今天,在实现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;
    }

这是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

如您所见,除了0,1,2,3以外的所有索引,背景颜色都应该改变。 但是,当活动开始时,除了第二行,第三行,第四行外,所有行都会更改其背景颜色(第一个行也会更改,尽管它的进度比0大)。 此外,当我再次上下滚动时,所有行似乎都改变了背景色,包括行号2,3,4。 我如何才能使行仅等于0的行更改其颜色?

在颜色变化中添加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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM