【十一年】注入框架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 ...
随机推荐
- 首款符合PICO-ITX规格的A20开源硬件开发平台
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NzM0MjcyMQ==&appmsgid=10001083&itemidx=2&am ...
- QT---系统托盘图标不显示原因
很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...
- Codeforces Round #270--B. Design Tutorial: Learn from Life
Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes in ...
- Sprite Kit教程:制作一个通用程序 2
注1:本文译自Sprite Kit Tutorial: Making a Universal App: Part 2 目录 动画的定义:可行性 属性列表 添加游戏逻辑 添加音效 何去何从 上一篇文章中 ...
- 如何使用Gmail的别名功能?
Gmail真的是一个很好的邮箱,一直是我的最爱!它有一个很独特的功能-别名,用这个功能,我们就可以把一个 邮箱当成很多个邮箱来使用了! 1.Gmail 不提供传统别名 ,但是你可以收到发送到 your ...
- mysql 执行计划走索引
<pre name="code" class="html">mysql> desc AssignClientManager; +------- ...
- 【Demo 0008】Java基础-抽象类
本掌学习要点: 1. 了解抽象类的定义及使用场景: 2. 掌握final修饰的类.方法以及变量作用及用法: 3. 掌握abstract 修饰的类及方法作用及用 ...
- 如何在使用摩托罗拉上的RSS阅读器应用进行一次订阅
订阅一个CSDN的RSS为例. 1.打开RSS阅读器. 2.设置->新增订阅->手动新增 订阅URL:输入http://articles.csdn.net/api/rss.php?tid= ...
- 《生活在Linux中》之:使用Bash就是使用Emacs
定义bash Emacs模式下的快捷键请参考: Readline-在BASH下自定义键盘热键 未完待续...
- LCS小结(O(∩_∩)O~吽吽)
LCS!~如果你在百度上搜这个的话会出来”英雄联盟冠军联赛”,orz..但是今天要讲的LCS是最长公共子序列 ,"Longest Common Subsequence "not&q ...