您的位置:首页 > 编程语言

从源代码理解创建LayoutInflater的路径

2016-04-10 22:46 288 查看
首先要明确的是LayoutInflater是一个抽象类;

我们先从LayoutInflater的使用说起:

常见的使用在ListView 的 getView()方法中:itemView = LayoutInflater.from(mContext).inflate(mLayoutId , null);

一.( LayoutInflater.from() -> Context.getSystemService() )

我们通过LayoutInflater的实现类的静态方法from(Context mContext )中得到一个LayoutInflater实例;

下面看from方法:在from()方法中,调用了Context类的getSystemService(String key)方法;

二.((Context)Activity.getSystemService() )

由于Application,Activity, Service都是Context的实现类,所以这里选取Activity为例子说明;要了解清楚Activity.getSystemSerivce(),得先从activity的创建说起:

在ActivityThread中的main()方法里,调用了attach()方法,在attach()中与ActivityManagerService通信从而调用了handleLaunchActivity()方法,而handleLaunchActivity()

转眼就调用了performLaunchActivity() :

1.创建Activity

2.创建Context对象,实现类是ContextImpl

· 3.获取Context对象

4.将appContext等对象attach到activity中

5.调用activity的onCreate()

这里我们主要看第二点,跟进到ContextImpl中:

public ContextImpl extends Context{
//ServiceFetcher顾名思义就是服务提取者,先拿到ServiceFetcher,就能由ServiceFetcher.getService(ContextImpl ctx)拿到服务;
static class ServiceFetcher {
public Object getService(ContextImpl ctx ) { }
public Object createService(ContextImpl ctx){}
}
//容器装着String与ServiceFetcher对
private static final HashMap<String , ServiceFetcher> SYSTEM_SERVICE_MAP = new HashMap<String , ServiceFetcher>();
//注册服务器
private static void registerService(String serviceName , ServiceFetcher fetcher ){};
//第一次加载该类就会把LAYOUT_INFLATER_SERVICE 与 它对应的ServiceFetcher对象注册进去
static {
registerService(LAYOUT_INFLATER_SERVICE , new ServiceFetcher{
<span style="white-space:pre"></span><p><span style="white-space:pre">		</span>public Object createService(ContextImpl ctx ){</p><p><span style="white-space:pre">		</span>return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());</p><span style="white-space:pre">		</span>});
}
//根据key获取对应的服务
public Object getSystemService( String name) {
ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
return fetcher== null? null:fetcher.getService(this);
}


到这里,我们终于看见了要找的getSystemService();

三.(PolicyManager.makeNewLayoutInflater() -> Policy.makeNewLayoutInflater();)

上面在注册LAYOUT_INFLATER_SERVICE的时候,创传进去一个内部类对象

new ServiceFetcher(){
public Object createService(ContextImpl ctx ){
return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext())
}}
这样我们跟进到PolicyManager类中看看:

static {
Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME):
sPolicy = (IPolicy ) policyClass.newInstance();
}


上面用反射构造了一个Policy类对象sPolicy , 这个Policy类实现了IPolicy接口,而PolicyManager实际上只是一个代理类,因为他的功能是由sPolicy来实现的,他不过是一个提供场地的类:

public static LayoutInflater makeNewLayoutInflater(Context ctx ) {
return sPolicy.makeNewLayoutInflater(ctx);
}


四.(Policy.makeNewLayoutInflater() -> PhoneLayoutInflater() )

接着我们跟进到Policy类中:

public class Policy implements IPolicy {
public LayoutInflater makeNewLayoutInflater(Context context ) {
return new PhoneLayoutInflater(context);
}}


这里我们知道LayoutInflater的实现类就是PhoneLayoutInflater;

到这里就完成了LayoutInflater的创建;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: