2013-02-18 65 views
1

Basicaly,我的問題是類似這樣的:滾動一個ListView改變TextView的顏色回原來的顏色

Scrolling a ListView changes everything from White to Black

唯一的區別是,我處理與滾動時改變顏色一個TextView (TextView在ListView中)。

我擡起頭,如果有一種方法類似於TextView的setCacheColorHint(Color.WHITE) - 我沒有找到它。

也許我應該動態設置默認的TextColor?因爲目前它正在以XML格式進行設置,然後在代碼中進行了更改。

我該如何處理?

代碼改變顏色爲藍色:

private void highlightSelectedFile(View vw) 
{ 
    TextView fileName = (TextView) vw.findViewById(R.id.file_name); 

    //Log.v("color: ", Integer.toString(fileName.getCurrentTextColor()));  

    if(fileName.getCurrentTextColor() == Color.BLACK) { 
     fileName.setTextColor(Color.BLUE); 

    } else { 
     fileName.setTextColor(Color.BLACK); 
     removeFromSelectedFiles(new File(fileName.getText().toString())); 
    } 
} 

這些TextView的去回黑色後,我滾動他們所在內的ListView:

ListView lv = (ListView) ac.findViewById(android.R.id.list); 
+0

發佈您的代碼。這將有助於其中一些人發佈答案。總是嘗試發佈一些代碼,以便其他人可以更清楚地理解你的問題。這將是一個證明,讓別人嘗試了一些東西。謝謝。 – Triode 2013-02-18 11:35:20

回答

0

在適配器的時候可能會使用在Adapter中將數據應用於您的觀點的數據項。當您滾動時,您的View被回收,並且您的getView()被稱爲另一個View對象。你的問題很容易解決。只需在您的數據項中添加一個int color;變量並像這樣使用它。

public View getView(int position, View convertView, ViewGroup parent) { 
    // efficient stuff here 

    // after applying your data 
    fileName.setTextColor(data.color); 
} 

也改變這個方法:

private void highlightSelectedFile(DataItem data) { 
    if(data.color == Color.BLACK) { 
     data.color = Color.BLUE; 
    } else { 
     data.color = Color.BLACK; 
     removeFromSelectedFiles(new File(fileName.getText().toString())); 
    } 
    // to update ListView 
    myAdapter.notifyDataSetChanged(); 
} 
+0

它沒有解決我的問題 - 我試過了。就像我說的,問題在於TextViews將顏色改回黑色,而不是ListView本身。 – Tool 2013-02-18 12:04:38

+0

好吧,我有你的問題,更新我的答案。 – 2013-02-18 12:09:13

0

只有把這個聲明在XML中的文件列表。 android:scrollingCache =「false」 它會解決你的問題。

+0

不工作 - 我猜它必須處理TextView在ListView中滾動期間呈現的方式。 – Tool 2013-02-18 12:14:16