安卓MVVM有如下特点:
- viewModel 的业务逻辑可以单独拿来测试
- 一个 view 对应一个 viewModel 业务逻辑可以分离,不会出现全能类
- 数据和界面绑定了,不用写垃圾代码,但是复用起来不舒服
先看下业务,一个输入框,当字符长度小于5时候提示,
“内容太短了,还不到5个字符”,字符长度大于10,提示“内容太长了,超过10个字符了”。
安卓MVVM是基于谷歌DataBinding 框架的,首先必须引入DataBinding,在app gradle 加入:
dataBinding {
enabled = true
}
1.View:
1.1 xml
xml 中更改databing 布局:alt+enter 传统布局提示更改为databind布局
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="ViewModel"
type="com.zrgj.study.mvvm.viewModel.ViewModel" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et"
android:hint="请输入内容"
android:layout_width="match_parent"
android:layout_height="60dp">
</EditText>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{ViewModel.hint}"
android:textSize="20sp"
android:textColor="@color/colorPrimary">
</TextView>
</LinearLayout>
</layout>
1.2 activity
public class MVVMActivity extends AppCompatActivity {
private ActivityMvvmBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_mvvm);
ViewModel viewModel = new ViewModel(getApplicationContext(), binding);
binding.setViewModel(viewModel);
}
}
2.Model
2.1 model
public class Content {
String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
2.2 Callback
public interface CallBack {
void contentTooLong();
void contentTooShort();
}
2.3 MVModel
public class MVModel {
public void create(Content content, CallBack calback){
if (content.getContent().length() > 10) {
calback.contentTooLong();
} else if (content.getContent().length() < 5) {
calback.contentTooShort();
}
}
}
3 ViewModel
public class ViewModel extends BaseObservable {
ActivityMvvmBinding binding;
private final MVModel mvModel;
public ViewModel(Context context, ActivityMvvmBinding binding) {
this.binding = binding;
mvModel = new MVModel();
dataChange();
}
@Bindable
public String getHint() {
return hint;
}
public void setHint(String hint) {
this.hint = hint;
notifyPropertyChanged(BR.hint);
}
String hint ="内容太短了,还不到5个字符";
public void dataChange(){
binding.et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String data =charSequence.toString().trim();
Content content = new Content();
content.setContent(data);
mvModel.create(content, new CallBack() {
@Override
public void contentTooLong() {
setHint("内容太长了,超过10个字符了");
}
@Override
public void contentTooShort() {
setHint("内容太短了,还不到5个字符");
}
});
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
整个业务理解我画了一张图:
1.Activity 创建DataBinding 视图和ViewModel 引用。
2.ViewModel持有Activity 和binding,继承、BaseObservable。
3.ViewModel在构造方法中创建MVModel 实例。
4.可以在Xml中引用 ViewModel变量和方法。
整个代码会清爽很多,依赖于框架能力,xml&Actiivty 不能复用。