【十一年】注入框架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 ...
 
随机推荐
- memset,memcpy,memmove,strcpy,strcat,strcmp的实现(其实很简单,每个程序都只有几行代码)
			
面试中的几个小问题 1.对stl中list封装(参考1): 2.对重要C函数实现(参考2): //memset void *memset(void *buffer, int c, int count) ...
 - Joomla3.1.1在64位win7下安装
			
将下载的joomla压缩包解压到Apache下的htdocs文件夹中. 打开Apache服务器,在浏览器输入http://localhost:8081/Joomla/ 即可进入Joolma的安装配置界 ...
 - boost参考博客
			
http://blog.csdn.net/caimouse/article/category/1339053
 - Python集成开发环境(Eclipse+Pydev)
			
刚開始学习python,就用Editplus, Notepad++来写小程序, 后来接触了Sublime Text2.认为很不错,没事写写代码.就用编辑器Sublime Text2,最好再配搭一个ap ...
 - NM_CUSTOMDRAW 消息
			
When the control first starts to paint itself, in response to a WM_PAINT, you receive a NM_CUSTOMDRA ...
 - boost asio 异步实现tcp通讯
			
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
 - linux pthread之学习篇
			
在应用程序编程中,为了不影响与用户交互的性能,通常需要创建新的线程来处理一些比较耗时的. 不影响用户体验的工作.而这又通常分为两种情况: (1)需要临时创建一个线程来做某件特定的事,等事情做完时线程即 ...
 - 记一个手游app数据文件的破解
			
出于一些非常猥琐的须要,同一时候自己也想做一些新奇的尝试,周末用了大半天时间破解了某款手游的数据文件. 过程比我预想的要顺利,主要原因还是我们开发者的懈怠.咳咳. 步骤例如以下: 下载安装包,解压,发 ...
 - ListView+CheckBox两种解决方式及原因分析
			
近期在用ListView+CheckBox搞一个item选中的项目,我将CheckBox的focus设置为false,另我大喜的是,CheckBox居然能够选中(窃喜中),这么简单就搞定了,由于数据量 ...
 - Windows消息队列
			
一 Windows中有一个系统消息队列,对于每一个正在执行的Windows应用程序,系统为其建立一个“消息队列”,即应用程序队列,用来存放该程序可能 创建的各种窗口的消息.应用程序中含有一段称作“消息 ...