【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
上一篇我们简单的介绍了一下RoboGuice的使用(【十】注入框架RoboGuice使用:(Your
First Testcase)),今天我们来看下自己定义View的注入(Custom View)。
在開始本文之前,你先要熟悉普通Java对象的注入(点击进入)。
在RoboGuice
3.0版本号中你相同给自己定义View(Custom View)进行诸如。
class MyView extends View {
@Inject Foo foo;
@InjectView(R.id.my_view) TextView myView; public MyView(Context context) {
inflate(context,R.layout.my_layout, this);
RoboGuice.getInjector(getContext()).injectMembers(this);
} public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflate(context,R.layout.my_layout, this);
RoboGuice.getInjector(getContext()).injectMembers(this);
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context,R.layout.my_layout, this);
RoboGuice.getInjector(getContext()).injectMembers(this);
} @Override
public void onFinishInflate() {
super.onFinishInflate();
//All injections are available from here
//in both cases of XML and programmatic creations (see below)
myView.setText(foo.computeFoo());
}
}
在Android中,我们能够通过两种方法进行创建View:
①:自己主动从XML进行载入
②:程序方法,使用new CustomView(Context context)进行创建
使用RoboGuice 3.0版本号,在不论什么情况下,你都要确保通过显式注入。获取来自RoboGuice的注入。
在Roboguice 3.1版本号,就会些差别:
Roboguice 3.1全然通过XML载入而且自己主动注入View中全部的子控件,在XML载入过程中须要注意:该会自己主动回调onFinishInflate()方法。然而假设你自己提供额外的构造函数,那么你须要负责通过RoboGuice进行注入。
在后一种情况中。在构造函数的末尾调用onFinishInflate()方法。
假设你希望使用RoboGuice模板,你能够更好的使用该回调方法来处理全部的构造函数。你的自己定义视图将会被注入。
以下是一个样例:
class MyView extends View {
@Inject Foo foo;
@InjectView(R.id.my_view) TextView myView; public MyView(Context context) {
inflate(context,R.layout.my_layout, this);
RoboGuice.getInjector(getContext()).injectMembers(this);
onFinishInflate();
} public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflate(context,R.layout.my_layout, this);
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context,R.layout.my_layout, this);
} @Override
public void onFinishInflate() {
super.onFinishInflate();
//All injections are available from here
//in both cases of XML and programmatic creations (see below)
myView.setText(foo.computeFoo());
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)的更多相关文章
- 【十三】注入框架RoboGuice采用:(Logging via Ln)
上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...
- 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)
上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...
- 【十】注入框架RoboGuice使用:(Your First Testcase)
上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
- 【四】注入框架RoboGuice使用:(Your First System Service Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...
- 【七】注入框架RoboGuice使用:(Your First Custom Binding)
上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...
- 【三】注入框架RoboGuice使用:(Your First Resource Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...
- 【八】注入框架RoboGuice使用:(Your First Injected Fragment)
上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 ...
- 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)
上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...
随机推荐
- javascript 交互取值
var publicClassName; var classIdInMemory = { lastVal: publicClassName, set:function(x){ if(x != &quo ...
- Inverse Quadratic Interpolation (website)
Inverse Quadratic Interpolation: https://www.youtube.com/watch?v=0H7mVPTLF7Q : https://www.youtube. ...
- delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)
前言:网上有很多介绍delphi创建闪屏的代码,大多只是在程序开启前显示一个闪屏,但是却没有说如何在闪屏上显示程序加载的进度,于是笔者有意思介绍一下这种闪屏方式. 1.创建一个窗体(TfrmSplas ...
- 【ASP.NET Web API教程】2.3.3 创建Admin控制器
原文:[ASP.NET Web API教程]2.3.3 创建Admin控制器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 3 ...
- 14.6.7?Limits on InnoDB Tables InnoDB 表的限制
14.6.7?Limits on InnoDB Tables InnoDB 表的限制 警告: 不要把MySQL system tables 从MyISAM 到InnoDB 表. 这是不支持的操作,如果 ...
- 大数据实时处理-基于Spark的大数据实时处理及应用技术培训
随着互联网.移动互联网和物联网的发展,我们已经切实地迎来了一个大数据 的时代.大数据是指无法在一定时间内用常规软件工具对其内容进行抓取.管理和处理的数据集合,对大数据的分析已经成为一个非常重要且紧迫的 ...
- Solaris 11的自动化安装(AI server)的搭建
solaris 11 总体比solaris 10很多变动的地方,可以去官方网站上面学习:http://www.oracle.com/technetwork/server-storage/solaris ...
- ScaleAnimation类:尺寸变化动画类
9.4 ScaleAnimation类:尺寸变化动画类 ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类.Scal ...
- Tomcat启动会遇到的问题部分解决方案
Tomcat的启动不一定会非常的顺利,这可以有多种原因 一.Tomcat是由Java所编写的,因此Tomcat的使用需要JDK的支持,如果没有配置环境变量,Tomcat当然无法启动,最明显的特征就是点 ...
- MSYS2 环境搭建(在Qt Creator可以设置环境变量来进行引用这些库)
本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...