Android MVVM框架RoboBinding初探

RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架。MVVM模式是MVC模式的重要更新,使得项目结构更加的优美,易于维护以及方便于测试。这也是为什么几个主流的语言都有相应的MVVM框架实现如windows phone,WPFSilverlight,JavaFX以及Flex。

在没有性能损失的前提下(使用AspectJ字节码生成来替代Java反射),RoboBinding 帮助你编写更可读,易于测试与维护的UI代码。框架具备以下优点:

  • 通过绑定移除 - 大量不必要的代码(如addXXListener(),findViewById()等) 。

  • 将 - 难于测试的Android代码以及运行过久且不切实际的Android单元测试 - 变为 pojo PresentationModels 及其普通的JUnit单元测试。

  • 提供对象类型Cursor来替换 - 关系类型Cursor,因为我们已经习惯于操作对象 。

  • 可以很容易的为任何自定义组件,第三方组件或Android widget编写属性绑定实现,简化代码,使项目易于维护。

从简单的角度看,他移除了如addXXListener(),findViewById()这些不必要的代码,连如BufferKnife那样的InjectView都不需要,因为你的代码一般不需要依赖于这些界面组件信息。下面以一个最简单的AndroidMVVM为例。

Layout:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2 
 3     xmlns:tools="http://schemas.android.com/tools"
 4 
 5     xmlns:bind="http://robobinding.org/android">
 6 
 7     <TextView
 8 
 9         bind:text="{hello}" />
10 
11         ...
12 
13     <Button
14 
15         android:text="Say Hello"
16 
17         bind:onClick="sayHello"/>
18 
19 </LinearLayout>

Presentation Model:

 1 public class PresentationModel extends AbstractPresentationModel {
 2 
 3     private String name;
 4 
 5     public String getHello() {
 6 
 7         return name + ": hello Android MVVM(Presentation Model)!";
 8 
 9     }
10 
11     ...
12 
13     public void sayHello() {
14 
15         firePropertyChange("hello");
16 
17     }
18 
19 }

Activity将layout与对应的presentation model绑定在一起。

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4 
 5     protected void onCreate(Bundle savedInstanceState) {
 6 
 7         ...
 8 
 9         PresentationModel presentationModel = new PresentationModel();
10 
11         View rootView = Binders.inflateAndBindWithoutPreInitializingViews(this, R.layout.activity_main, presentationModel);
12 
13         setContentView(rootView);
14 
15     }
16 
17 }

这样layout的{hello}与PresentationModel.hello绑定,layout的sayHello与PresenationModel.sayHello方法绑定。我们不需要在Layout中定义TextView, Button的Id因为我们不关心,且没有必要。当我们进一步观察时,我们发现PresentationModel是一个Pure POJO。这也是为什么软件界的泰斗Martin Fowler在2004年,提出了Presenation Model(MVVM) 模式。它是我们所熟悉的MVC的升级,进一步的把界面状态与逻辑解藕到Presentation Model中。我们可以通过以下几个示例项目学习RoboBinding使用,他们都可以直接导入Android Studio无需额外配置:

1.AndroidMVVM,最小的RoboBinding使用例子。

2.Album Sample,是Martin Fowler的Presentation Model模式原始例子基于RoboBinding的Android翻译版本。

3.Gallery,是用于展示RoboBinding的各种特性的使用包含Fragment, Menu, ViewPager等。

项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html

项目主页:http://www.open-open.com/lib/view/home/1410667351476

RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架。从简单的角度看,他移除了如addXXListener(),findViewById()这些不必要的代码,连如BufferKnife那样的InjectView都不需要,因为你的代码一般不需要依赖于这些界面组件信息。下面以一个最简单的AndroidMVVM为例。 Layout: <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: Presentation Model: public class PresentationModel extends AbstractPresentationModel {     private String name;     public String getHello() {         return name   ": hello Android MVVM(Presentation Model)!";     }     ...     public void sayHello() {         firePropertyChange("hello");     } } Activity将layout与对应的presentation model绑定在一起。 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);     } } 这样layout的{hello}与PresentationModel.hello绑定,layout的sayHello与PresenationModel.sayHello方法绑定。我们不需要在Layout中定义TextView, Button的Id因为我们不关心,且没有必要。当我们进一步观察时,我们发现PresentationModel是一个Pure POJO。这也是为什么软件界的泰斗Martin Fowler在2004年,提出了Presenation Model(MVVM) 模式。它是我们所熟悉的MVC的升级,进一步的把界面状态与逻辑解藕到Presentation Model中。我们可以通过以下几个示例项目学习RoboBinding使用,他们都可以直接导入Android Studio无需额外配置: 1.AndroidMVVM,最小的RoboBinding使用例子。 2.Album Sample,是Martin Fowler的Presentation Model模式原始例子基于RoboBindingAndroid翻译版本。 3.Gallery,是用于展示RoboBinding的各种特性的使用包含Fragment, Menu, ViewPager等。 项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html 标签:RoboBinding
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值