前言
MVVM是MVC的改进版,即M(Model数据层) - V(View视图层) - VM(ViewModel数据视图),和MVC最大的区别就是加了一个Binder,实现了View和ViewModel的双向绑定,Model和ViewModel的双向绑定。即:View改变了,Model会直接发生改变;Model改变了,View也会直接发生改变。
一、MVVM示意图
二、使用步骤
1.引入库
加入dataBinding,设置为真,这是实现双向绑定的库文件。
2.设置Model
新建一个User的类:
//Modle
//继承被观察者,实现数据发生改变,view能被更新
public class User extends BaseObservable {
private String name;
private String weight;
private String height;
public User() {
}
public User(String name, String weight, String height) {
this.name = name;
this.weight = weight;
this.height = height;
}
@Bindable//获取最新值
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);//数据发生改变,直接更新View
}
@Bindable
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
notifyPropertyChanged(BR.weight);
}
@Bindable
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
notifyPropertyChanged(BR.height);
}
}
3.设置View
<?xml version="1.0" encoding="utf-8"?>
<!--使用layout将view包起来-->
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--引入数据,设置名称和类型(包名)-->
<data>
<variable
name="user"
type="hk.com.whfgnbej.mvvm.User" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--使用@{user.name}显示数据-->
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:text="@{user.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--使用@={user.name}修改数据-->
<EditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/textView7"
android:text="@={user.name}"/>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@{user.weight}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@{user.height}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
4.设置ViewModel
public class MainActivity extends AppCompatActivity {
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取view
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
user = new User("张三","体重70千克","身高180厘米");
//给view添加数据
binding.setUser(user);
//数据发生改变,View会直接更新。
user.setName("李四");
}
}
总结
以上就是用具体代码实现MVVM的简单例子,也算初步认识MVVM了,而且ViewModel和MVC的ViewController相比更加简洁,减少了像
findViewById()这样的样本代码,减少了绑定View组件的过程,实现了自动更新View。大大简化了代码量。