从源代码理解创建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中:
到这里,我们终于看见了要找的getSystemService();
三.(PolicyManager.makeNewLayoutInflater() -> Policy.makeNewLayoutInflater();)
上面在注册LAYOUT_INFLATER_SERVICE的时候,创传进去一个内部类对象
上面用反射构造了一个Policy类对象sPolicy , 这个Policy类实现了IPolicy接口,而PolicyManager实际上只是一个代理类,因为他的功能是由sPolicy来实现的,他不过是一个提供场地的类:
四.(Policy.makeNewLayoutInflater() -> PhoneLayoutInflater() )
接着我们跟进到Policy类中:
这里我们知道LayoutInflater的实现类就是PhoneLayoutInflater;
到这里就完成了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的创建;
相关文章推荐
- Python学习之路--select解析
- Java多线程--并发和并行的区别
- c++string流之istringstream
- Machine Learning Logistic Regression and Newton's Method Andrew Ng 课程练习 Matlab Script 详细解析
- 【C#】机房重构版---开工
- JAVA String对象和字符串常量的关系解析
- RSA加密解密及数字签名Java实现
- 项目启动时,提示spring配置文件未知的utf-8编码
- Java与c++异同 小结
- 20145205 实验一 Java开发环境的熟悉
- 初级cnn研究辅助:python的matplotlib显示图片 之 按钮和触发事件
- 浅谈Java集合中Array(数组)的应用
- 20145204 《Java程序设计》第6周学习总结
- java签名证书
- 深入理解Java变量
- Java 数字签名原理及产生
- 结对编程--四则运算
- C#之数据序列化
- Struts2学习
- 20145335郝昊《java程序设计》第6周学习总结