PojoViewModel 这里先mark下,简要说下概念。
概要:实验性的,尝试 pojo风格的ViewModel。该子类无需 Observables 和 Commands。只要声明自己的 绑定接口就好。(实现PojoViewModel接口即可)
public interface PojoViewModel { PojoViewModelHelper getHelper(); void notifyPropertyChanged(String propertyName); }
it is only working in flat view models (i.e. Array, Cursor are not supported)
目前仅适用于1维数据模型操作。比如数组,游标之类的 是不支持的。
具体应用_ 简单来说,就是属性映射为pojo对应的 字段属性。方法映射为pojo定义的方法。
Demo:(登陆操作)
public class SamplPojoViewModel implements PojoViewModel { private PojoViewModelHelper mHelper = new PojoViewModelHelper(); public PojoViewModelHelper getHelper() { return mHelper; } public void notifyPropertyChanged(String propertyName) { mHelper.notifyPropertyChanged(propertyName); } // rest of the class declarations }
View Model :
public class LoginViewModel implements PojoViewModel { private PojoViewModelHelper mHelper = new PojoViewModelHelper(); public PojoViewModelHelper getHelper() { return mHelper; } public void notifyPropertyChanged(String propertyName) { mHelper.notifyPropertyChanged(propertyName); } // View Model Public contracts public String getLoginName() { return loginName; } public String getPassword() { return password; } // setters public void setLoginName(String loginName){ this.loginName = loginName; notifyPropertyChanged("LoginName"); } public void setPassword(String password){ this.password = password; notifyPropertyChanged("Password"); } public void Submit(){ ... } // Private fields private String loginName; private String password; // ... rest of the stuffs }
在Activity中 进行Binding时 需要对POJOViewModel进行 一层封装,因为Binder无法直接识别 PojoViewModel
PojoViewModelWrapper<?> wrapper = PojoViewModelWrapper.create(new SamplePojoViewModel()); Binder.setAndBindContentView(this, R.layout.main, wrapper);
对应Xml:
<TextView android:... binding:text="LoginName"/> <TextView android:... binding:text="Password"/> <Button android:... binding:onClick="Submit"/>