zoukankan      html  css  js  c++  java
  • 【十一年】注入框架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());
        }
    }


        

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    关于计算机改名无法连接TFS的问题
    配置 SQL Server 2008 Email 发送以及 Job 的 Notification通知功能
    javascript原型链中 this 的指向
    JavaScript中的构造函数 renturn
    Javascript中valueOf与toString区别
    JavaScript 中this与Dom中的注意
    SQL优化 CREATE STATISTICS
    Maven根据不同的环境打包不同的配置
    Maven build标签
    解决Maven报Plugin execution not covered by lifecycle configuration问题
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4944815.html
Copyright © 2011-2022 走看看