android中mvvm格式,Android中的MVVM架构设计实践和想法

在实际项目中很少用到MVVM,因为如果在一个团队中,本团队又是做一些需要快速成品的项目,那么让你放弃熟悉的MVP而去使用MVVM,这恐怕一般人都不愿意尝试,何况不是每个人都熟悉这框架,而且很有可能无法把控项目周期!但是任何困难都不应该是我们踏步不前的理由,早些时候就已经了解过MVVM,今天得空,做一些记录!

项目中使用mvvm

使用mvvm,必然会用到android为我们提供的dataBinding支持包

我们编写module中的build.gradle,添加这样的脚本就好了,如图:

e5cede185c87

build.gradle配置.png到这里,我们已经完成了项目中使用MVVM的初始化配置

项目中XML的布局文件变化

databinding框架有自己的一套布局规则,所以使用mvvm也不例外,下面看看布局如何来写

name="thrid"

type="com.booktest.mymvvm.ThridObservableItem"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@{thrid.strTextA}"/>

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text='@{thrid.boolTextB ==true ? "真的":"假的"}'/>

android:id="@+id/text3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@{thrid.strTextC}"/>

android:id="@+id/bt_change"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" I want to change"/>

细心的朋友应该都注意到了,布局跟以往的不一样了,这就是差异所在!

最外层用标签嵌套,

- 标签,这个标签其实就是让我们进行数据绑定的一个标签

- 标签里面分别有两个标签,分别来标识变量类型和变量名称

- 标签 标识变量类型,比如java.lang.String这就是String类型,com.booktest.mymvvm.ThridObservableItem 这个就是一个我自定义的一个User类型

- 标签 表示的就是我们定义的一个变量名称,这个变量名称我们会在下方的布局和对应的java代码中引用到

绑定的最基础的显示:@{name.字段}

ThridObservableItem.java类的两种实现:

Observable 双向绑定支持监听的数据类型 显示更新的内容,无需手动操作更新,只需更新数据源,直接让布局发生变化

public class ThridObservableItem {

public ObservableField strTextA=new ObservableField<>();

public ObservableBoolean boolTextB=new ObservableBoolean();

public ObservableField strTextC=new ObservableField<>();

//ObservableField 在获取数据的时候需要做一次调用

//如:String n = name.get()

//同理设置的时候也需要

//如:name.set("姓名");

//除了ObservableField,还有ObservableBoolean、ObservableInt ……

}

让Model实体继承自BaseObservable,并且对相应的属性的get()方法增加@Bindable注解,在set()方法中增加notifyPropertyChanged(BR.对应的控件id)属性,如果在BR.对应的控件id的时候提示找不到控件id,不要着急,Build一下工程即可!

public class ThridObservableItem2 extends BaseObservable {

private String strTextA;

private Boolean strTextB;

private String strTextC;

public ThridObservableItem2(String strTextA, Boolean strTextB, String strTextC) {

this.strTextA = strTextA;

this.strTextB = strTextB;

this.strTextC = strTextC;

}

@Bindable

public String getStrTextA() {

return strTextA;

}

public void setStrTextA(String strTextA) {

this.strTextA = strTextA;

notifyPropertyChanged(BR.strTextA);

}

@Bindable

public Boolean getStrTextB() {

return strTextB;

}

public void setStrTextB(Boolean strTextB) {

this.strTextB = strTextB;

notifyPropertyChanged(BR.strTextB);

}

@Bindable

public String getStrTextC() {

return strTextC;

}

public void setStrTextC(String strTextC) {

this.strTextC = strTextC;

notifyPropertyChanged(BR.strTextC);

}

}

哪个方式方便???自己琢磨!!!

Activity文件中的使用

public class Main3Activity extends AppCompatActivity {

private ActivityThridBinding binding;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

binding= DataBindingUtil.setContentView(this,R.layout.activity_thrid);

final ThridObservableItem thridObservableItem=new ThridObservableItem();

thridObservableItem.strTextA.set("这是Observable");

thridObservableItem.boolTextB.set(true);

thridObservableItem.strTextC.set("说的没错,确实如此");

binding.setThrid(thridObservableItem);

binding.btChange.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

thridObservableItem.strTextA.set("这是Observable");

thridObservableItem.boolTextB.set(false);

thridObservableItem.strTextC.set("说的不对");

}

});

}

}

DataBindingUtil.setContentView(this,R.layout.activity_thrid)设置布局内容;

可能大家发现了ActivityThridBinding,这个是类型名称怎么来的呢?不要着急,有规则,首先我们要知道我们使用的布局名称,我这里引用的布局是:R.layout.activity_thrid,然而我的Activity的binding类型名称是ActivityThridBinding,是的,有联系!

activity_thrid.xml布局文件,经过DataBindingUtil生成的类是ActivityThridBinding类

规则:就是按照布局名的顺序依次输出,最后边加上Binding即可

我们还可以查看工程目录下通过DataBindingUtil生成的类:

e5cede185c87

DataBindingUtil生成的类.png

如果我们的XML布局中没有运用dataBinding的标签嵌套,是不会生成这些类的,也就无法实例。

最终效果如下:

e5cede185c87

双向绑定效果.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值