Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-12-13:请说说依赖注入框架ButterKnife的实现原理? #211

Open
Moosphan opened this issue Dec 13, 2019 · 3 comments
Open

Comments

@Moosphan
Copy link
Owner

No description provided.

@WislieZhu
Copy link

WislieZhu commented Jan 5, 2020

1.通过注解器在编译期间生成一个XX_ViewBinding.java文件(XX可以是activity,fragment,adapter,dialog),这个文件这么生成的?
注解器里会添加需要类型的注解;
查找XX类中的特定类型注解,如果有,拼接成字符串,创建并写到XX_ViewBinding.java文件中
2.XX_ViewBinding.java会持有XX的引用,
如果是初始化控件,通过xx.findViewById实现
如果是设置监听,类似xx.setOnClickListener实现
3.XX类中初始化XX_ViewBinding对象,这样打通了整个流程

@fenggeZhang
Copy link

fenggeZhang commented Jan 6, 2020

注解技术,在编译阶段生成java代码
(1)开始它会扫描java代码中所有的ButterKnife注解 onbindview onclickVIew
(2)ButterknifeProcessor ->$$ViewBinder
(3)调用bind方法加载生成的ViewBInderl类

@mlinqirong
Copy link

使用ButterKnife是为了简化代码 通过注解代替findViewById
ButterKnife是在编译时注解 不会影响APP的效率和性能
ButterKnife的实现原理是通过注解和注解器对每个Activity生成一个相对应的类 把原本需要findViewById和点击事件利用注解生成代码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants