I understand that the purpose of Android\'s data-binding library is for views to observe data and automatically update when that data changes.
Question:
Use @Bindable annotation and the Observable interface. It avoids the boilerplate code and u can use it for primitive data types.
/**
* Created by Amardeep on 11/2/16.
*/
public class Cart implements Observable {
private final PropertyChangeRegistry mPropertyChangeRegistry =
new PropertyChangeRegistry();
@Bindable
private int itemCount;
@Bindable
private String name;
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
mPropertyChangeRegistry.notifyChange(this, BR.itemCount);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
mPropertyChangeRegistry.notifyChange(this, BR.name);
}
@Override
public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
mPropertyChangeRegistry.add(callback);
}
@Override
public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
mPropertyChangeRegistry.remove(callback);
}
}