• AB_PojoViewModel


    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"/>
  • 相关阅读:
    js,jQuery实现可关闭悬浮框广告特效,兼容(谷歌,火狐,Ie)
    各种选择框jQuery的选中方法
    表单校验demo
    两种方法实现城市级联菜单
    树形菜单
    匿名函数和鼠标移入移除事件
    多线程实例
    Lock锁
    Oracle语句
    Java NIO
  • 原文地址:https://www.cnblogs.com/pinotao/p/3298290.html
Copyright © 2020-2023  润新知