0
我一直在试验Android数据绑定库,其中包括谷歌开发人员指南。但即使严格遵循其代码notifyPropertyChanged()
也不适用。Android数据绑定mCallback始终为空
BaseObservable
中的mCallbacks始终为空。我已经调试了代码,因为绑定已经建立,并且调用了addOnPropertyChangedCallback
并设置了mCallbacks,但由于某种原因,当您调用notifyPropertyChanged()
时,此引用已丢失。
有可能是我失踪的东西,所以任何帮助将不胜感激!
代码:
public class TestActivity extends AppCompatActivity {
TestModel mTestModel;
ActivityTestBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_test);
mTestModel = new TestModel("Test", "User");
mBinding.setTestModel(mTestModel);
mBinding.ratingBtnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTestModel.setFirstName("New");
mTestModel.setLastName("WOOOOORKS");
mBinding.notifyPropertyChanged(BR.firstName);
}
});
}}
public class TestModel extends BaseObservable {
private String firstName;
private String lastName;
public TestModel(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Bindable
public String getFirstName() {
return this.firstName;
}
@Bindable
public String getLastName() {
return this.lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}}
布局:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="testModel"
type="com.boundless.happymeter.model.TestModel"
/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{testModel.firstName}"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{testModel.lastName}"
/>
<Button
android:id="@+id/rating_btn_update"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Update"
/>
</LinearLayout>
</layout>
注: 它可以强制使用mBinding.invalidateAll()的结合 - 但这是一个相当难看soloution
这工作了魅力使用
notifyPropertyChanged();
!我正在使用mBinding.notifyPropertyChanged(而不是BaseObservable中的超级方法),所以回调当然是null,谢谢! – enyciaa