• 【十一年】注入框架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());
        }
    }


        

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

  • 相关阅读:
    编码和解码总结
    编码和解码程序例
    IO流字节流输入输出格式
    递归经典题
    ArrayList去重
    集合
    接口文档
    Vue项目零碎知识(全局js,css配置,element-UI,bs使用, img动态配置,js数组)
    djang项目中的疑问及解决办法(ValueError: Invalid model reference 'apps.user.User'. String model references must be of the form 'app_label.ModelName'.)
    Django项目中出现的错误及解决办法(ValueError: Dependency on app with no migrations: customuser)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4944815.html
Copyright © 2020-2023  润新知