2013-12-12 227 views
0

我應該使用convertView還是在下面的方法中創建一個新的視圖?我應該使用convertView嗎?

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.content, null); 
    return v; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.content, null); 
    return convertView; 
} 

回答

2

充氣每次View是從性能的角度來看一個錯誤的決定。只有在convertView爲空的情況下,常見模式纔會使您的視圖膨脹。所以,請使用系統提供給您的視圖。這樣你就可以啓用一些允許不浪費內存的魔法優化(查看回收)

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.content, null); 
    } 
    return convertView; 
} 
+0

null檢查是必須的嗎? – Nfear

+1

它不是強制性的,但強烈建議 – Blackbelt

+0

我需要在支票外創建文字瀏覽? – Nfear