美文网首页
Android中的MVVM架构设计实践和想法

Android中的MVVM架构设计实践和想法

作者: 大川的川 | 来源:发表于2018-12-28 18:08 被阅读0次

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

  • 项目中使用mvvm

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

我们编写module中的build.gradle,添加这样的脚本就好了,如图: build.gradle配置.png 到这里,我们已经完成了项目中使用MVVM的初始化配置
  • 项目中XML的布局文件变化

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

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="thrid"
            type="com.booktest.mymvvm.ThridObservableItem"/>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{thrid.strTextA}"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{thrid.boolTextB ==true ? "真的":"假的"}'/>
        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{thrid.strTextC}"/>
        <Button
            android:id="@+id/bt_change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" I want to change"/>
    </LinearLayout>
</layout>

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

最外层用<layout>标签嵌套,
- <data>标签,这个标签其实就是让我们进行数据绑定的一个标签
- <variable>标签里面分别有<type> <name>两个标签,分别来标识变量类型和变量名称
- <type>标签 标识变量类型,比如java.lang.String这就是String类型,com.booktest.mymvvm.ThridObservableItem 这个就是一个我自定义的一个User类型
- <name>标签 表示的就是我们定义的一个变量名称,这个变量名称我们会在下方的布局和对应的java代码中引用到

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

ThridObservableItem.java类的两种实现:
  1. Observable 双向绑定支持监听的数据类型 显示更新的内容,无需手动操作更新,只需更新数据源,直接让布局发生变化
public class ThridObservableItem {
    public ObservableField<String> strTextA=new ObservableField<>();
    public ObservableBoolean boolTextB=new ObservableBoolean();
    public ObservableField<String> strTextC=new ObservableField<>();
    //ObservableField 在获取数据的时候需要做一次调用
    //如:String n = name.get()
    //同理设置的时候也需要
    //如:name.set("姓名");
    //除了ObservableField,还有ObservableBoolean、ObservableInt ……
}
  1. 让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生成的类: DataBindingUtil生成的类.png
如果我们的XML布局中没有运用dataBinding的<layout>标签嵌套,是不会生成这些类的,也就无法实例。

最终效果如下: 双向绑定效果.gif

相关文章

网友评论

      本文标题:Android中的MVVM架构设计实践和想法

      本文链接:https://www.haomeiwen.com/subject/hwcelqtx.html