没有使用DataBinding时
MainActivity.java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
myViewModel = new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewModel.add(1);
}
});
myViewModel.getNumber().observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
textView.setText(String.valueOf(integer));
}
});
}
MyViewModel代码:
public MutableLiveData<Integer> getNumber() {
if (number ==null){
number = new MutableLiveData<>();
number.setValue(0);
}
return number;
}
public void add (int n){
number.setValue(number.getValue()+n);
}
使用DataBinding的步骤:
先在gradle module中android{}内添加一段话:
dataBinding{
enabled true
}
之后进入到activity_main.xml文件中的代码
点击左上角的黄色灯泡选择convert to data binding layout
系统会生成
layout
data
/data
/layout
两个标签
这时我们可以注释掉onCreate中的setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
然后补充
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);来代替上面注释掉的这段话
这时xml中的标签、关键词都会成为类的一个成员
所以可以继续去除声明变量和变量的定位函数
//TextView textView;
//Button button;
//textView = findViewById(R.id.textView);
//button = findViewById(R.id.button);
再在之前说到的data标签中添加
textview改写为:
android:text="@{String.valueOf(data.number)}"
再为button添加一个函数:
android:onClick="@{()->data.add(1)}"
补充:
@{} 大括号之间填写java代码
()->是调用函数
现在,调用函数的操作我们就在xml中完成了,所以我们可以注释掉mainActivity.java中的两个函数
添加
binding.setData(myViewModel);
binding.setLifecycleOwner(this);
两句话,代码修改完毕
最终mainAcitivity.class代码变成:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
myViewModel = new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);
binding.setData(myViewModel);
binding.setLifecycleOwner(this);
Activity_main.xml文件代码变为:
<?xml version="1.0" encoding="utf-8"?><data>
<variable
name='data'
type='com.example.databinding.MyViewModel'
/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.number)}"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.222" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
android:onClick="@{()->data.add(1)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>