2013-06-06 111 views
0

我有以下問題。從資源創建新對象並修改資源會改變資源

我有兩個片段,他們每個人都有一個ListView與適配器。通過從第一個列表中選擇元素,我將包含第一個列表的片段替換爲包含第二個列表的片段。我想讓每一行背景只在第二個Fragment中透明,但下面的代碼在第二個適配器的getView中使用,它看起來像更新資源?誰能解釋爲什麼?

代碼:

第一塊碎片:

public View getView(int position, View convertView, ViewGroup parent) { 
(...) 
    v.setBackgroundResource(R.drawable.abstract_gray); 
(...) 
} 

第二塊碎片:

public View getView(int position, View convertView, ViewGroup parent) { 
(...) 
    Drawable backgroundDrawable = context.getResources().getDrawable(R.drawable.abstract_gray); 
    backgroundDrawable.setAlpha(ToolsAndConstants.BACKGROUND_TRANSPARENCY); 
    v.setBackgroundDrawable(backgroundDrawable); 
    // I call this to check if when I call the drawable from resurce it will not be transparent 
    v.setBackgroundResource(R.drawable.abstract_gray); 
    // But it is...So previous fragment background, when I go back with back Button 
(...) 
} 

也許這個問題是非常基本的,但是當我創建資源新的對象,我實際上使用了資源本身,並且對新對象所做的所有更改都將在af中進行完整的應用程序,即使其他類不會訪問該對象?

編輯:

對不起,我才意識到,我並不創造新的繪製對象,我只是做一個參考。那我該如何創建新的?如果我不能,我怎樣才能改變第二個列表的背景?

回答