2012-08-13 20 views
7

La mia domanda è qual è il modo migliore per creare un'istanza LayoutInflater? C'è qualche differenza traCreazione efficiente di un layoutInflater

LayoutInflater inflater = LayoutInflater.from(context); 

e

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

che è la soluzione migliore? Altre soluzioni sono anche benvenute.

Grazie.

risposta

10

Se è stato selezionato il file di origine LayoutInflater.java.

/** 
* Obtains the LayoutInflater from the given context. 
*/ 
public static LayoutInflater from(Context context) { 
    LayoutInflater LayoutInflater = 
      (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (LayoutInflater == null) { 
     throw new AssertionError("LayoutInflater not found."); 
    } 
    return LayoutInflater; 
} 
+1

Quindi la seconda soluzione dovrebbe essere un po 'efficiente rispetto al Primo. –

+0

Giusto. Grazie. – overbet13