作用
- 將視圖綁定到Activity不再需要findViewById()
- DataBinding 與 ViewModle+LiveData 將可觀察數(shù)據(jù)反向綁定到View,此時可以實現(xiàn)一個結(jié)構(gòu)化較好的程序
使用
一脚作、將視圖綁定到Activity
1. build.gradle 啟用數(shù)據(jù)綁定
android {
......
dataBinding {
enabled = true
}
}
2. xml中增加layout標(biāo)簽
<?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文件名相對應(yīng)的類例如 activity_main.xml 會生成 ActivityMainBinding 類
3. Activity中創(chuàng)建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直接調(diào)用view葫哗,但這樣還不夠簡潔還需要.setText、setOnClickListener等操作設(shè)置數(shù)據(jù)球涛,通過下一步可以反向綁定將數(shù)據(jù)直接綁定到界面上
二劣针、DataBinding 與 ViewModle+LiveData 將可觀察數(shù)據(jù)反向綁定
- 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 數(shù)據(jù)加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標(biāo)簽中引用了一個ViewModel,給TextView綁定數(shù)據(jù)liveDataIndex亿扁,在Button中綁定點擊事件調(diào)用addLiveDataIndex方法捺典,addLiveDataIndex執(zhí)行l(wèi)iveDataIndex數(shù)據(jù)變更,則界面自動更新从祝。此時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); //委托當(dāng)前類管理生命周期
return binding.getRoot();
}
- 如果希望Fragment獲取Activity的ViewModel 使用requireActivity()
MyViewModle myViewModle = new ViewModelProvider(requireActivity()).get(MyViewModle.class);