i have the situation that i have a fragment, whose textfields will be filled by either a X1Object or a X2Object, which both are implementing the interface IXObject and extending the BaseObservable class provided by the Android DataBinding library, but contain additional different fields and behaviour. The IXObject contains the methods for getters and setters.
public interface IXObject {
void setName(String name);
String getName();
}
public class X1Object extends BaseObservable implements IXObject {
private String name;
@Override
@Bindable
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
}
public class X2Object extends BaseObservable implements IXObject {...}
Then i am trying to use a single layout file for the fragment, using Android DataBinding. The layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable name="xObject" type="com.test.x.model.IXObject"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{xObject.name}"/>
</LinearLayout>
</layout>
In the fragment class, i am applying the binding:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
MyFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_fragment, container, false);
View view = binding.getRoot();
IXObject xobj = new X1Object();
binding.setXObject(xobject);
return view;
}
Unfortunately, using the IXObject as data binding reference, the method "addOnPropertyChangedCallback" in the BaseObservable class is never called.
Using X1Object directly for binding in the layout file and the binding class, everything works perfectly.
Can you help me how to achieve my goal to use an interface for binding?
Thank you.