Data Binding的一些问题:
- 观察数据更新的时候 无论当前activity是否显示在前台都会去更新数据。lifecycles 在onpause之后不会更新UI,重新进入onresume之后才会把之前的值更新到UI中。
- 如果有嵌套的时候get方法不能重名,见下面代码如果ViewModel和User类同时有绑定
getTime()
的方法,则只有一个能收到消息,改ViewModel的方法为getTime1()
就好了.
<com.example.shibu.myapplication.MyLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<TextView
android:id="@+id/btn"
android:text='@{""+vm.user.time}'//time
android:layout_width="200px"
android:layout_height="200px" />
<TextView
android:id="@+id/btn1"
android:text='@{""+vm.time1}'//修改为time1和上面的time作区分
android:layout_width="200px"
android:layout_height="200px" />
</com.example.shibu.myapplication.MyLayout>
public class ViewModel extends BaseObservable{
public User user = new User();
@Bindable
public int getTime1() { //修改为getTime1 和User里面的方法做区分
Log.d("bushi",""+time);
return time;
}
public void setTime1(int time) {
this.time = time;
notifyPropertyChanged(BR.time1);
}
public int time = 0;
}
public class User extends BaseObservable {
@Bindable
public int getTime() {
return time;
}
public void setTime(int name) {
this.time = name;
notifyPropertyChanged(BR.time);
}
int time;
}
- 关于
ViewStub
的databinding。ViewStub
和别的view
有一些不一样,ViewStub
一开始并不可见的,当它被使用之后ViewStub
会从视图层级中消失。所以使用android.databinding.ViewStubProxy
对象来监听[ViewStub.OnInflateListener]
布局代码段如下
view_stub.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="vm" type="com.example.shibu.myapplication.ViewModel"/>
</data>
<com.example.shibu.myapplication.MyLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/btn"
android:text='@{""+vm.user.time}'
android:layout_width="200px"
android:layout_height="200px" />
<TextView
android:id="@+id/btn1"
android:text='@{""+vm.time1}'
android:layout_width="200px"
android:layout_height="200px" />
</com.example.shibu.myapplication.MyLayout>
</layout>
activity_main.xml
<ViewStub
android:id="@+id/viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/view_stub"/>
</android.support.constraint.ConstraintLayout>
绑定代码段如下:
final ViewModel viewModel = new ViewModel();
viewModel.user.setTime(time);
final ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
activityMainBinding.viewstub.setOnInflateListener(new ViewStub.OnInflateListener() {
@Override
public void onInflate(ViewStub stub, View inflated) {
ViewStubBinding viewDataBinding = DataBindingUtil.bind(inflated);
viewDataBinding.setVm(viewModel);
}
});
activityMainBinding.viewstub.getViewStub().inflate();