RoboBinding简介

RoboBinding是一款基于Android的数据绑定组件,它可以帮助你编写可读性强、容易测试以及性能优越的Android UI应用。RoboBinding有以下几个特点:

  • 为了精简框架,RoboBinding移除了大量不必要的代码,比如addXXListener(),findViewById()等。
  • 可以将难以测试的Android代码转换为普通的JUnit测试。
  • 提供对象类型Cursor来替换 -关系类型Cursor,因为我们已经习惯于操作对象。
  • 可以很容易的为任何自定义组件,第三方组件或Android widget编写属性绑定实现,简化代码,使项目易于维护。

下面我们通过一个小例子来学习RoboBinding的使用方法。

RoboBinding使用方法

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:bind="http://robobinding.org/android">    <TextView        bind:text="{hello}" />        ...    <Button        android:text="Say Hello"        bind:onClick="sayHello"/></LinearLayout>

presentation models:

@org.robobinding.annotation.PresentationModelpublic class PresentationModel implements HasPresentationModelChangeSupport {    private String name;    public String getHello() {        return name + ": hello Android MVVM(Presentation Model)!";    }    ...    public void sayHello() {        firePropertyChange("hello");    }}

Activity代码

Activities将应用布局和展现层数据绑定在一起,MainActivity.java的代码如下:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        ...        PresentationModel presentationModel = new PresentationModel();        View rootView = Binders.inflateAndBindWithoutPreInitializingViews(this, R.layout.activity_main, presentationModel);        setContentView(rootView);    }}

更多关于RoboBinding的使用,可以访问其在Github上的主页。

转自:http://www.codeceo.com/article/android-robobinding.html

更多相关文章

  1. android 4.0 "移动网络" 选项 不存在
  2. Android——用XML的selector实现按钮多态
  3. android使用豆瓣API出现500错误及解决方法
  4. android 项目 local_Test_exam 代码分享
  5. Android(安卓)jni代码注册本地方法
  6. Intent详解 (一) : 显式Intent
  7. mldn andoird
  8. [置顶] Android四大组件之广播接收器(三)
  9. 使用 Android(安卓)Jetpack 加快应用开发速度

随机推荐

  1. 深入理解Java类加载机制(一)
  2. Android推送通知的实现--PHP+ANDROID做消
  3. [轉]Android的内存泄漏和调试
  4. Android(安卓)中查看内存的使用情况集常
  5. TextView和EditText的介绍
  6. android字间距实现,textview字间距实现
  7. 一点见解: Android嵌套滑动和NestedScrol
  8. Android 用户界面---菜单(Menus 一)
  9. [Android Studio 权威教程]断点调试和高级
  10. 更适合Android的集合 SparseArray/ArrayM