簡體   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