¿Cuál es la forma correcta de obtener inflador de diseño en Android?


Hay una manera de obtener LayoutInflater:

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

Y otra manera es:

LayoutInflater inflater = LayoutInflater.from(context);

Una tercera (cuando estoy en una Actividad) es:

LayoutInflater inflater = getLayoutInflater();

Entonces, ¿cuál es la diferencia entre ellos?

Tenga en cuenta que cuando envié el tercer inflador a mi adaptador, mi aplicación funcionó. Pero cuando me envió el contexto y creó el inflater a través de la segunda manera, no!

Author: sachin10, 2014-01-08

4 answers

No hay mucha diferencia entre ellos.

Como dice el doc Objeto abstracto público getSystemService (Nombre de cadena)

Un LayoutInflater para inflar recursos de layout en este contexto.

Y para el LayoutInflater estático público de (Context context)

Obtiene el LayoutInflater del contexto dado.

Puedes comprobar este hilo Si hay alguna diferencia entre getLayoutInflater () y .getSystemService (Context.LAYOUT_INFLATER_SERVICE)

 5
Author: Community,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:32:26

Utilizar fuera de su actividad

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

Dentro de su actividad

     LayoutInflater inflater = getLayoutInflater();

Compruebe esto

Si abre la fuente de Android, puede ver que el LayoutInflator.desde el método se ve así:

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

Y no hay diferencia

Mientras la Actividad o Ventana que llama a getLayoutInflater() tenga el mismo Contexto que llamaría a getSystemService(), no hay diferencia.

 17
Author: sachin10,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-23 05:20:00

La única diferencia es el contexto que utiliza. Si el contexto que usas con LayoutInflater.fromContext() o context.getSystemService(...) es realmente una Actividad, debería ser equivalente a Activity.getLayoutInflater(). Si es el objeto de la aplicación, es posible que tenga problemas para inflar las vistas que contienen fragmentos, IIRC.

 4
Author: MathewI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-08 12:11:35

En realidad creo que el getLayoutInflater() - Método de Actividad es un método de conveniencia.

Recuerde que Activity subclases Context, por lo que todos los Métodos disponibles dentro de Context también están disponibles en la Clase Activity.

Internamente habrá una llamada a LayoutInflater.fromContext() o context.getSystemService(), así que me apegaría a context.getSystemService tanto para evitar la llamada innecesaria al método como para aclarar que estoy haciendo una llamada a un servicio del sistema.

 2
Author: Peter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-23 05:21:01