上一篇我们简单的介绍了一下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)的更多相关文章

  1. 【十三】注入框架RoboGuice采用:(Logging via Ln)

    上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...

  2. 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)

    上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...

  3. 【十】注入框架RoboGuice使用:(Your First Testcase)

    上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...

  4. 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

    上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...

  5. 【四】注入框架RoboGuice使用:(Your First System Service Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...

  6. 【七】注入框架RoboGuice使用:(Your First Custom Binding)

    上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...

  7. 【三】注入框架RoboGuice使用:(Your First Resource Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...

  8. 【八】注入框架RoboGuice使用:(Your First Injected Fragment)

        上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解     ...

  9. 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)

    上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...

随机推荐

  1. js 常用方法记事本

    1.获取被选中行的名称<tab选项卡中为iframe> /* S 获取首页被选中的选项卡名称 */ var currTab = $("#layout_center_tabs&qu ...

  2. Spring Boot 分布式Session状态保存Redis

    在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失. 常规的解决方 ...

  3. android设置背景图片透明

    设置Activiyt为透明可以在Activity中引用系统透明主题android:theme="@android:style/Theme.Translucent" 设置背景图片透明 ...

  4. <转载>CSS解决图片过大撑破DIV的方法

    DIV+CSS网页内容中如果插入大于DIV层宽度显示,过大的图片将会撑破网页宽度显示从而网页严重变形,您是否遇到过?这里DIVCSS5给大家介绍几种解决图片撑破撑开网页DIV层方法. 图片撑破宽度解决 ...

  5. SetWindowLong

    SetWindowLong函数介绍 收藏 SetWindowLong函数介绍 SetWindowLong Unicode 函数原型 LONG SetWindowLong(hwnd,nIndex,lNe ...

  6. 用yum查询想安装的软件

    1.使用YUM查找软件包  命令:yum search~  2.列出所有可安装的软件包  命令:yum list  3.列出所有可更新的软件包  命令:yum list updates  4.列出所有 ...

  7. unity中怎样获取全部子物体的组件

    public GameObject[] obj;     void Awake()     {             for (int i = 0; i < obj.Length; i++) ...

  8. java jquery 函数多參数传递

    业务需求: 名次   伙伴 业绩 签单 面谈 每日目标 1 文彬 5100 6 10 查看目标 2 马红月 4550 4 6 查看目标 3 王刚 4100 3 9 查看目标 4 郭亚凯 3450 4 ...

  9. Delphi事件的广播2

    上篇文章写了将事件分离成类的方法来实现事件的广播,这次将参考观察者模式来实现事件的广播.模式中主要有这两个角色: 发布者:发布者保存着一张观察者的列表,以便在必要的时候调用观察者的方法. 观察者:观察 ...

  10. Delphi结构中使用String时遇到的内存泄露问题(没有利用String的引用计数自动销毁字符串的功能)

    先定义一个结构: TUserInfo = record  UserID: Integer; // 用户编号  UserName: string; // 用户名end; 然后编写如下代码: proced ...