2014-03-28 103 views
2

我有一個列表視圖,其中我顯示文件和文件夾列表。 我用我的getView方法listview項目的滾動背景顏色變化,

static class ViewHolder { 
    protected TextView text1; 
    protected TextView text2; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder viewHolder = null; 

    if(convertView == null){ 

     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
     convertView = inflater.inflate(R.layout.row, parent, false); 

     viewHolder = new ViewHolder(); 
     viewHolder.text1 = (TextView) convertView.findViewById(R.id.text1); 
     viewHolder.text2 = (TextView) convertView.findViewById(R.id.text2); 

     convertView.setTag(viewHolder); 

    } 
    else{ 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    viewHolder.text1.setText(itemsArrayList.get(position).getFileName()); 
    viewHolder.text2.setText(itemsArrayList.get(position).getSize()); 

    <if(itemsArrayList.get(position).isHidden()) { 
     convertView.setBackgroundColor(context.getResources().getColor(R.color.hiddenColor)); 
    } 

    return convertView; 
} 

如果文件/文件夾是隱藏的,我改變列表項的hiddenColor的背景顏色,
(默認的背景色是XML)

但在滾動它將幾乎所有的列表項目背景顏色設置爲隱藏顏色。

我知道這是由於listview回收,但不知道如何解決它。

+0

檢查這個參考http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct。點擊時更改文字和顏色。 – Raghunandan

回答

6

您必須設置非隱藏顏色,因爲如果視圖被重用,您將在轉換視圖之前設置hiddenColor。

if(itemsArrayList.get(position).isHidden()) { 
    convertView.setBackgroundColor(context.getResources().getColor(R.color.hiddenColor)); 
} else { 
    **convertView.setBackgroundColor(Put your other color here)** 
} 
+0

謝謝,它工作 –

0

android:fadingEdge="none"在XML

+0

這已棄用 –

0

嘗試添加該XML文件

android:cacheColorHint="@android:color/transparent" 
+0

我早些時候嘗試過,但它不起作用 –

0

你有誇大佈局爲每個項目,並返回一個新的視角。 Listview對其他項目使用相同的視圖。

刪除if(convertView == null),以便每個項目都有不同的視圖對象。

其他的方法是添加在查看持有人的位置,並檢查

if(convertView == null || contentView.getTag().position != position) 
+0

如果我們將每個項目膨脹每次它會給內存異常和應用程序將滾動一些後崩潰。 我曾經嘗試過這個。 –

+0

沒有列表視圖將保持只在內存中的項目。這種方法用於處理不同動作的高級列表項(例如,如果列表項必須在內部滾動其測試) – Libin