2012-07-11 51 views
0

我有一個列表查看與部分和條目。例如..Android ListView部分轉黑色

Supplies (Section Item) 
    Pens  (Entry Item) 
    Pencils 
    Groceries 
    Eggs 
    Lettuce 
    Etc.... 

在我的列表適配器中,我通過這樣設置了各個部分項的背景。

view.setBackgroundColor(Color.YELLOW); 

這一切工作正常,直到我開始滾動,然後將部分項目變成黑色(輸入項不這樣做)。任何人都知道如何防止這一點?

代碼getView()方法

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    final ItemEchelon i = items.get(position); 
    if (i != null) { 
     if(i.isSection()){ 
      SectionItemEchelon si = (SectionItem)i; 
      v = vi.inflate(R.layout.item_section, null); 

      v.setOnClickListener(null); 
      v.setOnLongClickListener(null); 
      v.setLongClickable(false); 

      final TextView sectionView = (TextView) v.findViewById(R.id.tv_Name); 
      sectionView.setText(si.getTitle()); 
      if(count == 0) 
      { 
       v.setBackgroundColor(Color.YELLOW); 
      } 
      if(count == 1) 
      { 
       v.setBackgroundColor(Color.GREEN); 
      } 
      if(count == 2) 
      { 
       v.setBackgroundColor(Color.RED); 
      } 
      if(count == 3) 
      { 
       v.setBackgroundColor(Color.GRAY); 
      } 
      count ++; 
     }else{ 
      EntryItemEchelon ei = (EntryItemEchelon)i; 
      v = vi.inflate(R.layout.item_entry, null); 
      final TextView title = (TextView)v.findViewById(R.id.tv_entryTitle); 
      final TextView score = (TextView)v.findViewById(R.id.tv_entryScore); 

      if (title != null) 
       title.setText(ei.Name); 
      if(score != null) 
       score.setText(ei.Score); 
     } 
    } 
    return v; 
} 

修正通過去除問題,如果用於改變背景顏色語句。這樣做,所以你必須在構造一個SectionItem時初始化一個顏色。

回答

1

使用這種android:cacheColorHint="@android:color/transparent"List View

+0

嘗試過,但沒有奏效。 – 2012-07-11 18:22:45

+0

然後發佈您的'getView'方法的代碼。 – 2012-07-11 18:24:35

+0

我更新了問題。 – 2012-07-11 18:40:09

0

這個屬性添加到列表視圖

android:cacheColorHint="#00000000" 

比都將是完美:)。

謝謝

+0

試過了,這沒有奏效。 – 2012-07-11 18:24:16

+0

你能分享你的listView嗎? – SALMAN 2012-07-11 18:25:38

0

變量「計數」是你的問題?看起來你正在增加它,當滾動時,你忽略了convertView,所以總是創建一個新的視圖。如果count> = 4,則不會獲得背景...

應該使用convertView來提高效率,並幫助解決此問題。

if (convertView != null) 
v = convertView; 
else 
{ 
v = vi.inflate(); 
... 
} 

另一大危險,如果我正確地讀你的代碼,是你依靠getView被稱爲以特定的順序,第1部分第2節之前,我不會那樣做的......使用int position參數來確定節。

+0

是的,我消除了這一點 – 2012-07-12 17:27:48