由于想理清项目的设计思路,从MVC模式看到了MVP模式,再到MVVM模式。大体上把安卓常用的模式过了一下,现整理下看MVVM模式时接触的DataBinding知识点。
本文主要为转载,所以请稳步参考文献:
https://www.cnblogs.com/ldq2016/p/6698181.html 《Android DataBinding库(MVVM设计模式)》
本文结束!!!!
下文是学习时参考着文献自己试手的代码,
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="MyClassName">
<import type="com.sxc.mvvmdemo.bean.Crsh"/>
<variable
name="myUserObj"
type="com.sxc.mvvmdemo.bean.Crsh"/>
<variable
name="clickEvent"
type="com.sxc.mvvmdemo.click.OnClick"/>
<variable
name="crsh2"
type="com.sxc.mvvmdemo.bean.Crsh2"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sxc.mvpdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{myUserObj.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@{crsh2.name}"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text='@{"" + crsh2.age}'/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@{String.valueOf(crsh2.age)}"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/edit_text_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/edit_text_pass"/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="登录"
android:id="@+id/sub_login_btn"/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="DataBinding"
android:onClick="@{clickEvent.showToast}"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/http_web_view"/>
</LinearLayout>
</layout>
private MyClassName binding;
private Crsh crsh;
private Crsh2 crsh2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
// setContentView(R.layout.activity_main);
crsh = new Crsh("crsh");
binding.setMyUserObj(crsh);
binding.setClickEvent(new OnClick());
crsh2 = new Crsh2();
binding.setCrsh2(crsh2);
crsh2.name.set("fuck!!!!");
crsh2.age.set(18);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
crsh.setName("曹88888");
// binding.setMyUserObj(crsh);
}
},5000);
}
public class Crsh2 {
public ObservableField<String> name = new ObservableField<>();
public ObservableInt age = new ObservableInt();
}
public class Crsh extends BaseObservable{
private String name;
public Crsh(String name) {
this.name = name;
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
}
public class OnClick {
public void showToast(View view) {
Toast.makeText(view.getContext(),"hello world",Toast.LENGTH_LONG).show();
}
}