大杀器-RoboBinding

来源:互联网 发布:怎么避免mac涂层脱落 编辑:程序博客网 时间:2024/05/21 04:24

介绍

一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架。 在没有性能损失的前提下(使用源代码生成来替代Java反射),RoboBinding 帮助你编写更可读,易于测试与维护的UI代码。

  • 通过绑定移除大量不必要的代码(如addXXListener(),findViewById()等) 。
  • 将难于测试的Android代码以及运行过久且不切实际的Android单元测试 变为 pojo PresentationModels 及其普通的JUnit单元测试。
  • 提供对象类型Cursor来替换 - 关系类型Cursor,因为我们已经习惯于操作对象 。
  • 可以很容易的为任何自定义组件,第三方组件或Android widget编写属性绑定实现,简化代码,使项目易于维护。

官网: http://robobinding.org

有中文翻译版本,关于详细介绍,移步官网,下面主要填一下使用Android Studio配置时的一些坑.

根据官方介绍,我使用了AspectJ(AS上不好配置),也可以不用
1.配置 app/build.gradle

buildscript{    repositories{        mavenCentral()        maven(){            name 'RoboBinding AspectJPlugin Maven Repository'            url "https://github.com/RoboBinding/RoboBinding-aspectj-plugin/raw/master/mavenRepo"        }    }    dependencies {        classpath 'com.android.tools.build:gradle:1.+'        classpath 'org.robobinding:aspectj-plugin:0.8.4'        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'    }}

注意需要引用v4,我是在libs添加的jar包

apply plugin: 'com.android.application'apply plugin: 'com.neenbedankt.android-apt'apply plugin: 'org.robobinding.android-aspectj'
    compile ("org.robobinding:robobinding:0.8.9:with-aop-and-dependencies") {        exclude group: 'com.google.guava', module: 'guava'    }    aspectPath ("org.robobinding:robobinding:0.8.9:with-aop-and-dependencies") {        exclude group: 'com.google.guava', module: 'guava'    }    apt "org.robobinding:codegen:0.8.9"    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
repositories {    jcenter()    mavenCentral()    maven() {        name 'SonaType snapshot repository'        url 'https://oss.sonatype.org/content/repositories/snapshots'    }}
android {    ....    //这里是禁止xml中使用bind命名空间时报错的    lintOptions {        disable 'UnusedAttribute', 'ValidFragment', 'GradleDependency', 'OnClick', 'MissingPrefix', 'MenuTitle'    }    ...}

以上片段是需要注意的

还有就是网络问题,在公司的网络Gradle死活都编译不通,回到家立马就好了
接下来就是使用了


以在Fragment中使用为例(官方的例子用法很全了)
1. fragment_demo.xml

<LinearLayout     //加上命名空间    xmlns:bind="http://robobinding.org/android"<TextView    bind:text="{content}"

点击事件 bind:onClick=”methodName”
还支持其他更多属性的绑定

  1. DemoPresentationModel.java
//加上注解@PresentationModelpublic class DemoPresentationModel{    private String content; //对应xml中bind:text="{content}"    public String getContent(){        return content;    }    public String setContent(Content content){        this.content = content;    }}
  1. DemoFragment.java
    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        presentationModel = new DemoPresentationModel();        presentationModel.setContent("内容");    } @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        ViewBinder viewBinder = new BinderFactoryBuilder().build().createViewBinder(getActivity());        View rootView = viewBinder.inflateAndBind(R.layout.fragment_demo, presentationModel);   //xml里面按照上面的要求,不能写错        return rootView;    }

需要更新属性的,让POJO类实现HasPresentationModelChangeSupport

//加上注解@PresentationModelpublic class DemoPresentationModel implements HasPresentationModelChangeSupport{    private String content; //对应xml中bind:text="{content}"    private PresentationModelChangeSupport changeSupport;     public DemoPresentationModel(){         changeSupport = new PresentationModelChangeSupport(this);     }    public String getContent(){        return content;    }    public String setContent(Content content){        this.content = content;    }    public void updataContent(String newContent){        changeSupport.firePropertyChange("content");//更新content属性    }}

然后我们调用 updataContent来更新视图


一下子简化好多代码和逻辑,还有其他更强大的功能,官网观看,还有中文的视频

0 0