Android 的MVVM模式的简单应用

来源:互联网 发布:淘客cms系统 编辑:程序博客网 时间:2024/05/16 19:26

Android 的MVVM模式,大家应该有所听过,我就不说这个MVVM的解释了,我说一下它的特点,那就是数据注入方便,而且能把数据和View绑定起来,十分方便。举个例子:将一个User的类创建对象A并设置数据,然后将这个对象A.name与一个TextView的text属性绑定,然后这个TextView就会显示A.name的数据,然后改变这个A.name的时候,这个TextView显示的数据也会跟着变化
以下是例子代码:
首先在app的build.gradle里添加

    dataBinding {        enabled true}

其他的代码是

public class User extends BaseObservable{    private String userName;    private String realName;    private String mobile;    private int age;    /**     * 注意: 在BR里对应的常量为follow     */    private boolean isFollow;    public User(String realName, String mobile) {        this.realName = realName;        this.mobile = mobile;        Log.d("User", "user construct invoked");    }    public User() {    }    @Bindable    public boolean isFollow() {        return isFollow;    }    public void setIsFollow(boolean isFollow) {        this.isFollow = isFollow;        notifyPropertyChanged(BR.follow);    }    @Bindable    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;        notifyPropertyChanged(BR.userName);    }    @Bindable    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;        notifyPropertyChanged(BR.age);    }    @Bindable    public String getRealName() {        return realName;    }    public void setRealName(String realName) {        this.realName = realName;        notifyPropertyChanged(BR.realName);    }    @Bindable    public String getMobile() {        return mobile;    }    public void setMobile(String mobile) {        this.mobile = mobile;        notifyPropertyChanged(BR.mobile);    }}
public class UpdateUserActivity extends AppCompatActivity {    private ActivityUpdateUserBinding binding;    private User user;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        binding = DataBindingUtil.setContentView(this, R.layout.activity_update_user);        user = new User("Chiclaim", "119");        binding.setUser(user);    }    //如果(某个)字段发生变化.    //1,通过User继承BaseObservable实现    //2,通过ObservableField方式实现    //3,通过Observable Collections的方式 如:ObservableArrayMap    //4,当然可以通过binding.setUser(user) [相当于所有的View重新设置一遍]    public void updateNameByPOJP(View view) {        if ("Johnny".equals(user.getRealName())) {            user.setRealName("Chiclaim");            user.setMobile("110");        } else {            user.setRealName("Johnny");            user.setMobile("119");        }    }}
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android">    <data>        <import type="android.databinding.ObservableMap"/>        <variable            name="user"            type="com.mvvm.model.User"/>    </data>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:padding="16dp">        <LinearLayout            android:id="@+id/ll_pojo"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="1dp"            android:background="#21000000"            android:orientation="horizontal"            android:padding="10dp">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@null"                android:text="@{user.realName}"                android:textSize="14dp"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:background="@null"                android:text="@{user.mobile}"                android:textSize="14dp"                android:textStyle="bold"/>        </LinearLayout>        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="updateNameByPOJP"            android:text="修改(By POJO)"            android:textSize="14dp"/>    </LinearLayout></layout>

再见

原创粉丝点击