RoboBinding:一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架
2016-02-16 14:50
726 查看
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:onClick="sayHello"/>
</LinearLayout>
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模式原始例子基于RoboBinding的Android翻译版本。
3.Gallery,是用于展示RoboBinding的各种特性的使用包含Fragment, Menu, ViewPager等。
项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html
github 地址https://github.com/RoboBinding/RoboBinding
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:onClick="sayHello"/>
</LinearLayout>
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模式原始例子基于RoboBinding的Android翻译版本。
3.Gallery,是用于展示RoboBinding的各种特性的使用包含Fragment, Menu, ViewPager等。
项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html
github 地址https://github.com/RoboBinding/RoboBinding
相关文章推荐
- Android Json解析
- Android HAL实例解析
- Android 系统上一款开源的图表库
- AndroidAsync :基于nio的异步通信库
- Android日常开发60条经验
- Android ListView GridView 错位
- android findviewbyId 简化写法
- Android后台任务队列类库
- Android 自定义控件打造史上最简单的侧滑菜单02
- Android中的图像处理
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 性能优化
- Android 数据持久化(SQLite数据存储)
- Android中如何禁用其他应用,并且不在桌面显示
- Android studio插件GsonFormat,返回json快速创建实体对象
- Android Annotation基础教程
- android5.1后新增shadow$_monitor_和shadow$_klass_ ,导致反射无效
- Android studio无法发现nexus 5x解决方法
- android布局之线性布局LinearLayout(总结)