DataBinding+ViewModel+LiveData+Room+Rxjava的使用之二DataBinding
DataBinding的数据绑定和简单使用
其一xml
<TextView
android:id="@+id/waht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
activity或者fragment里赋值
binding.waht.setText("我来设置属性");
其二xml中的数据绑定
public class Car {
public String car_top;
public String car_bottom;
public String getCar_top() {
return car_top;
}
public void setCar_top(String car_top) {
this.car_top = car_top;
}
public String getCar_bottom() {
return car_bottom;
}
public void setCar_bottom(String car_bottom) {
this.car_bottom = car_bottom;
}
}
xml中用法
<layout>
<data>
<variable
name="bean"
type="moc.ludomym.ym.main_xianglong.Car"/>
<!--type (bean类) name xml中使用的别名-->
</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"
tools:context="moc.ludomym.ym.main_xianglong.MainActivity">
<TextView
android:id="@+id/waht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{bean.car_top}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</layout>
其中数据绑定可以使有@{bean.car_top}直接来使用 这里面配合着room和livedata特别方便
下一篇:
servlet实现layui的搜索功能
