Android数据绑定组件RoboBinding使用详解
2016-04-02 20:51
531 查看
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.PresentationModel public 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上的主页。
转自:/article/1212822.html
相关文章推荐
- 一个Android图片(视频)选择器:android-media-picker
- 了解RxJava之Android响应式编程(四)
- Android 表单验证框架:AValidations
- Android 轻量级输入校验库:Fire Eye
- Android引入即用的便捷开发框架WelikeAndroid
- 高质量 Android 开发框架 LoonAndroid 详解
- Android动画原理分析
- 如何用Fiddler对Android应用进行抓包
- Android studio推荐插件以及升级后插件丢失问题解决
- Android开发模式
- android线程间通信机制loop
- 高效Android开发者必须知道的4个工具
- Android-ViewPager中调用notifyDataSetChanged失效问题--setItemPosition--POSITION_NONE
- Android ndk使用介绍
- 关于 android基础知识点整理
- android:interpolator
- VideoView无法播放此视频 android问题
- Android SQLite调试
- Android Material Design 兼容库的使用详解
- Android ExpandableListView 进行二级扩展 (BaseExpandableListAdapter)