【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
2015-11-07 12:18
441 查看
上一篇我们简单的介绍了一下RoboGuice的使用(【十】注入框架RoboGuice使用:(Your
First Testcase)),今天我们来看下自己定义View的注入(Custom View)。
在開始本文之前,你先要熟悉普通Java对象的注入(点击进入)。
在RoboGuice
3.0版本号中你相同给自己定义View(Custom View)进行诸如。
①:自己主动从XML进行载入
②:程序方法,使用new CustomView(Context context)进行创建
使用RoboGuice 3.0版本号,在不论什么情况下,你都要确保通过显式注入。获取来自RoboGuice的注入。
在Roboguice 3.1版本号,就会些差别:
Roboguice 3.1全然通过XML载入而且自己主动注入View中全部的子控件,在XML载入过程中须要注意:该会自己主动回调onFinishInflate()方法。然而假设你自己提供额外的构造函数,那么你须要负责通过RoboGuice进行注入。
在后一种情况中。在构造函数的末尾调用onFinishInflate()方法。
假设你希望使用RoboGuice模板,你能够更好的使用该回调方法来处理全部的构造函数。你的自己定义视图将会被注入。
以下是一个样例:
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()); } }
相关文章推荐
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备三)
- UGUI 图片去色,取灰色
- iOS UIDatePicker使用
- play 集成squeryl ORM持久化框架
- [LintCode]Find Median of Unsorted Array O(n) quick sort
- Longest Consecutive Sequence 解答
- play2.0 集成 squeryl
- 触摸事件UITouch的用法
- UIButton内部子控件自定义布局-“UIEdgeInsets”
- 4.Arduino IDE代码如下
- UIBarButtonItemSystemItemFixedItem设定指定宽度的barButtonItem
- hdoj 2767 Proving Equivalences【强连通&&tarjan】
- IOS UISegmentedControl 分段控制器
- UIScrollView的代理(delegate)
- UIScrollView作为第一个初始化View,导致其内的第一个UIimageView只显示一半的高度
- codeforces-446A-DZY Loves Sequences
- explaining of genotyping and DNA resequencing and variant calling
- SQL Interview Question
- 使用代码提示miui系统开启悬浮框
- Codeforces Round #328 (Div. 2)B. The Monster and the Squirrel