RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架。从简单的角度看,他移除了如addXXListener(),findViewById()这些不必要的代码,连如BufferKnife那样的InjectView都不需要,因为你的代码一般不需要依赖于这些界面组件信息。
项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html
项目github 地址https://github.com/RoboBinding
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://robobinding.org/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingPrefix" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
bind:text="{hello}" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
bind:text="${name}" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hello"
bind:onClick="sayHello" />
</LinearLayout>
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PresentationModel presentationModel = new PresentationModel();
ViewBinder viewBinder = createViewBinder();
View rootView = viewBinder.inflateAndBind(R.layout.activity_test,
presentationModel);
setContentView(rootView);
}
private ViewBinder createViewBinder() {
BinderFactory reusableBinderFactory = new BinderFactoryBuilder()
.build();
return reusableBinderFactory.createViewBinder(this);
}
}
@org.robobinding.annotation.PresentationModel
public class PresentationModel implements HasPresentationModelChangeSupport {
private PresentationModelChangeSupport changeSupport;
private String name;
public PresentationModel() {
changeSupport = new PresentationModelChangeSupport(this);
}
public String getHello() {
return name + ": hello Android MVVM(Presentation Model)!";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello() {
changeSupport.firePropertyChange("hello");
}
@Override
public PresentationModelChangeSupport getPresentationModelChangeSupport() {
return changeSupport;
}
}