大殺器-RoboBinding,殺器-RoboBinding
介紹
一個實現了資料繫結 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”
還支援其他更多屬性的綁定
//加上註解@PresentationModelpublic class DemoPresentationModel{ private String content; //對應xml中bind:text="{content}" public String getContent(){ return content; } public String setContent(Content content){ this.content = content; }}
@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來更新視圖
一下子簡化好多代碼和邏輯,還有其他更強大的功能,官網觀看,還有中文的視頻