Android MVVM databidng 数据双向绑定与事件绑定

MVVM的好处与特性不再过多讨论。

使用MVVM也有很多辅助框架,我用的是Activity+javabean+handler的模式,基本上是可以数据双向绑定与事件绑定的普通需求。

首先在xml布局文件中添加

<data>
    <variable
        name="loginHandler"
        type="com.webtest.handler.LoginHandler"/>
    <variable
        name="loginInfo"
        type="com.webtest.model.LoginInfo" />
</data>

其中LoginInfo是我们建的一个实体类用于操作数据,LoginHandler则作为viewModel层进行业务的处理。

在xml中需要绑定数据的地方添加绑定

<EditText
    android:id="@+id/login_edit_verification"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="手机号"
    android:gravity="center"
    android:textColorHint="#7FFFFF"
    android:textColor="#FFFFFF"
    android:text="@={loginInfo.verification}"/>

注意"@={loginInfo.verification}"要有“=”,如果没有只能单向传递数据(从UI获取数据,不能将数据同步更新到UI)有等号才可以实现双向绑定。

在需要绑定事件的方添加

<Button
    android:id="@+id/login_get_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/get_button"
    android:layout_marginTop="6dp"
    app:layout_constraintEnd_toEndOf="@+id/login_edit_phontno"
    app:layout_constraintTop_toBottomOf="@+id/login_edit_phontno"
    android:onClick="@{loginHandler.onClickGet}"/>

xml布局文件里需要做的事情就这么多。然后写一个javabean类来实现数据绑定

public class LoginInfo extends BaseObservable {
    private String phoneno;
    private String verification;

    public LoginInfo(){

    }

    public LoginInfo(String phoneno, String verification){
        this.phoneno = phoneno;
        this.verification = verification;
    }

    @Bindable
    public String getPhoneno() {
        return phoneno;
    }

    public void setPhoneno(String phoneno) {
        this.phoneno = phoneno;
        notifyPropertyChanged(BR.phoneno);
    }

    @Bindable
    public String getVerification() {
        return verification;
    }

    public void setVerification(String verification) {
        this.verification = verification;
        notifyPropertyChanged(BR.verification);
    }
}

注意:在get方法上添加注解@Bindable,在set方法中添加

notifyPropertyChanged(BR.verification);其中BR有两个包,import导错的话会报错,报错可以试着换一个BR的包。

然后在Activity的oncreate()方法中添加

ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
binding.setLoginInfo(loginInfo);

就可以实现双向绑定了。
其中ActivityLoginBinding是根据R.layout.activity_login自动生成的,如果报错可以尝试clean下或者重启。
最好是将activity_login考过去然后将下划线去掉将首字母改成大写一般就会提示需要import的包了。

实现事件绑定则是建一个Hanler类,写上事件的方法就可以进行事件处理了

public void onClickGet(View view){
   Toast.makeText(view.getContext(),"给你验证码",Toast.LENGTH_SHORT).show()
}

就可以实现事件绑定。

由于我们通常会在Handler类里进行数据操作,而数据通常是从UI即Activity中获取,所以要自己在Hanler类中写个javabean的get和set方法

public LoginInfo getLoginInfo() {
    return loginInfo;
}
public void setLoginInfo(LoginInfo loginInfo) {
    this.loginInfo = loginInfo;
}

LoginInfo loginInfo;

然后在Activity中调用

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    loginInfo = new LoginInfo();
    LoginHandler loginHandler = new LoginHandler();
    loginHandler.setLoginInfo(loginInfo);
    ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);

    binding.setLoginHandler(loginHandler);
    binding.setLoginInfo(loginInfo);
}

这样就是Activity只做UI相关的设置等,所有的业务操作全在Handler类中进行。

猜你喜欢

转载自blog.csdn.net/qq_37980878/article/details/82740333
今日推荐