当前位置: 代码迷 >> 综合 >> LayoutInflater是如何“移花接木”-上篇
  详细解决方案

LayoutInflater是如何“移花接木”-上篇

热度:6   发布时间:2023-12-16 04:01:45.0

开篇之前,先唠叨唠叨为何用移花接木,移花接木,指使用手段更换事物来欺骗他人,不过开始想到的是偷梁换柱,暗渡陈仓,还有暗送秋波这些词,仔细查阅了下资料,无奈,

两者大都是贬义词,后者又是献媚取宠、暗中勾结之类的含义,也是贬义,本着怜香惜玉的原则,最终选择移花接木这个词,为标题所用。

说到LayoutInflater,很多人说熟悉,它将我们的xml布局转化为对应的view,看呐,xml转换为view,多么神奇,犹如魔术师一般,移花接木,这个词很贴切

切入正题,分析下LayoutInflater是如何上演“移花接木”魔术的

来看看,获取LayoutInflater的方式,如下:

        1.使用Contex上下文,获取服务

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

        2.使用Activity获取

                xxxActivity.this.getLayoutInflater();

        3.使用LayoutInfalter静态方法

                LayoutInflater.from(Context context);

以上是我们经常使用的几种获取LayoutInflater的方法,接下来,看看它们内部做了些什么

1.使用Contex上下文

Context本身是抽象类,需要找到子类ContextImpl,重写的getSystemService

Context.java

    @Overridepublic Object getSystemService(String name) {return SystemServiceRegistry.getSystemService(this, name);}
这里有个SystemServiceRegistry类,拥有getSystemService(ContextImpl ctx, String name)方法,代码如下:

SystemServiceRegistry.java

    public static Object getSystemService(ContextImpl ctx, String name) {ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);return fetcher != null ? fetcher.getService(ctx) : null;}public static String getSystemServiceName(Class<?> serviceClass) {return SYSTEM_SERVICE_NAMES.get(serviceClass);}

    private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES = new HashMap<Class<?>, String>();private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = new HashMap<String, ServiceFetcher<?>>();//静态代码快
    static{//省略...registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,new CachedServiceFetcher<LayoutInflater>() {@Overridepublic LayoutInflater createService(ContextImpl ctx) {return new PhoneLayoutInflater(ctx.getOuterContext());}});//...}
private static <T> void registerService(String serviceName, Class<T> serviceClass, ServiceFetcher<T> serviceFetcher) { SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName); SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher); }



从上面看来,

SYSTEM_SERVICE_FETCHERS是一个map集合,存储的是ServiceFetcher对象

static代码块,在类加载的时候会被加载,此时,registerService被调用,注册LayoutInflater服务,注册的服务会被缓存到SYSTEM_SERVICE_FETCHERS中,便于在

getSystemService的时候获取

PhoneLayoutInflater实际上是LayoutInfalter的子类,在CreateService方法中被创建并返回

static块中要注册的服务很多,这里只保留一个,具体可以看SystemServiceRegistry源码

那么,SystemServiceRegistry类在哪里用到呢,如果了解ContextImpl代码的话,就容易了,会有以下代码,mServiceCache是成员变量,它的定义是为了后面代码的执行,

无其他用处。

ContextImpl.java

    //The system service cache for the system services that are cached per-ContextImpl.final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
SystemServiceRegistry.java
    public static Object[] createServiceCache() {return new Object[sServiceCacheSize];}

Activity启动,创建Context上下文实例时,createServiceCache()被执行,从而SystemServiceRegistry的static代码被加载到内存,各种服务注册并缓冲到集合中,方便在其他地

方使用某个服务时,从缓冲集合中获取。也就是,在context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)时,对应的是从SYSTEM_SERVICE_FETCHERS缓冲

集合中取出

2.使用Activity获取
Activity.java
    public LayoutInflater getLayoutInflater() {return getWindow().getLayoutInflater();}
getWindow()方法获取的是Activity中的mWindow成员变量,该变量在Activity的attach方法中初始化,Actiivty在启动时会调用attach方法

    public Window getWindow() {return mWindow;}final void attach(Context context, ActivityThread aThread,...省略参数//...mWindow = new PhoneWindow(this);//...}

接下来看看PhoneWindow类的getLayoutInflater(),如下:

PhowWindow.java

    public LayoutInflater getLayoutInflater() {return mLayoutInflater;}
public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); }



继续看LayoutInflater类,

LayoutInflater.java

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

到了这里,是不是感觉很熟悉,没错,这里获取getLayoutInflater对象方式,最终与第一种方式是一样的

3.使用LayoutInfalter静态方法

LayoutInflater.java

    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;}
实际上,还是第一种获取getLayoutInflater对象方式一样

也就是说,我们常用的获取LayoutInflater对象方法,最终都是通过getSystemService系统服务的方式获取的