2012-08-13 19 views
7

Moje pytanie brzmi, jaki jest najlepszy sposób utworzenia instancji LayoutInflater? Czy jest jakaś różnica międzyEfektywne tworzenie LayoutInflater

LayoutInflater inflater = LayoutInflater.from(context); 

i

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

co jest lepszym rozwiązaniem? Inne rozwiązania również są mile widziane.

Dzięki.

Odpowiedz

10

Po sprawdzeniu pliku źródłowego LayoutInflater.java można znaleźć.

/** 
* 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

Więc drugie rozwiązanie powinno być trochę wydajniejsze niż pierwsze. –

+0

Dobrze. Dziękuję Ci. – overbet13