作用
- 将视图绑定到Activity不再需要findViewById()
- DataBinding 与 ViewModle+LiveData 将可观察数据反向绑定到View,此时可以实现一个结构化较好的程序
使用
一、将视图绑定到Activity
1. build.gradle 启用数据绑定
android {
......
dataBinding {
enabled = true
}
}
2. xml中增加layout标签
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</layout>
此时会自动生成与xml文件名相对应的类例如 activity_main.xml 会生成 ActivityMainBinding 类
3. Activity中创建binding对象
private LayoutBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.layout);
binding.tv.setText("文本");
binding.but.setOnClickListener(v -> {
//...
});
}
省略了findViewById 通过binding.id直接调用view,但这样还不够简洁还需要.setText、setOnClickListener等操作设置数据,通过下一步可以反向绑定将数据直接绑定到界面上
二、DataBinding 与 ViewModle+LiveData 将可观察数据反向绑定
- ViewModel
public class MyViewModle extends ViewModel {
// public int index = 0;
private MutableLiveData<Integer> liveDataIndex;
//获取liveDataIndex类型
public MutableLiveData<Integer> getLiveDataIndex() {
if (null == liveDataIndex) {
liveDataIndex = new MutableLiveData<>();
liveDataIndex.setValue(0);
}
return liveDataIndex;
}
//liveDataIndex 数据加1
public void addLiveDataIndex() {
getLiveDataIndex().setValue(liveDataIndex.getValue() + 1);
}
}
- xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="data"
type="com.example.modle.MyViewModle" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.liveDataIndex)}" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{()->data.addLiveDataIndex(1)}" />
</LinearLayout>
</layout>
variable标签中引用了一个ViewModel,给TextView绑定数据liveDataIndex,在Button中绑定点击事件调用addLiveDataIndex方法,addLiveDataIndex执行liveDataIndex数据变更,则界面自动更新。此时Activity不在需要setText、setListener()
- Activity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyViewModle myViewModle = new ViewModelProvider(this).get(MyViewModle.class);
LayoutBinding binding = DataBindingUtil.setContentView(this, R.layout.layout);
binding.setData(myViewModle); // 此处的setData是布局variable name="data"
binding.setLifecycleOwner(this);
}
- Fragment
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
MyViewModle myViewModle = new ViewModelProvider(this).get(MyViewModle.class);
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_about, container, false);
binding.setModel(myViewModle );
binding.setLifecycleOwner(this); //委托当前类管理生命周期
return binding.getRoot();
}
- 如果希望Fragment获取Activity的ViewModel 使用requireActivity()
MyViewModle myViewModle = new ViewModelProvider(requireActivity()).get(MyViewModle.class);