13. Notifying view #1
public class User extends BaseObservable {
private String firstName;
public User(String firstName) {
this.firstName = firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
@Bindable
public String getFirstName() {
return this.firstName;
}
}
14. Notifying view #2
public class User {
public final ObservableField<String> firstName;
public User(String name) {
this.firstName = new ObservableField<>(firstName);
}
public void setName(String firstName) {
this.firstName.set(firstName);
}
}
19. Binding Component
public class MyDataBindingCoponent implements DataBindingComponent
{
public EditTextBindings getEditTextBindings()
{
return new EditTextBindings();
}
}
27. RecyclerView Binding #3
public class UsersViewModel extends BaseObservable
{
@Bindable
public ObservableArrayList<UserViewModel> users;
public UsersViewModel()
{
this.users = new ObservableArrayList<>();
}
public void addUser(String name, String surname)
{
this.users.add(new UserViewModel(new User(name, surname)));
}
}
28. RecyclerView Binding #4
public ItemBinder<UserViewModel> itemViewBinder()
{
return new CompositeItemBinder<UserViewModel>(
new SuperUserBinder(BR.user, R.layout.item_super_user),
new UserBinder(BR.user, R.layout.item_user)
);
}
30. How it works?
Bind ViewModel to
View
Register for
property change
callbacks
Trigger
notifyPropertyChanged
Request rebind
Execute pending
bindings
User input or from
code
31. How it works?
public void setViewModel(TwoWayViewModel viewModel) {
updateRegistration(0, viewModel);
this.mViewModel = viewModel;
synchronized(this) {
mDirtyFlags |= 0x1L;
notifyPropertyChanged(BR.viewModel);
super.requestRebind();
}
32. How it works?
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
if ((dirtyFlags & 0x7L) != 0) {
if (viewModel != null) {
colorViewModel = viewModel.getColor();
}
if ((dirtyFlags & 0x7L) != 0) {
ColorPickerViewBindings.setColor(this.colorpicker, colorViewModel);
EditTextBindings.setText(this.mboundView2, colorViewModel);
}
if ((dirtyFlags & 0x4L) != 0) {
ColorPickerViewBindings.setColorListener(this.colorpicker, null, colorpickercolorAttr);
TextViewBindingAdapter.setTextWatcher(this.mboundView2, null, null, null, mboundView2androidTe);
this.mboundView3.setOnClickListener(mCallback2);
}
}
46. Two-Way data binding
public class TwoWayViewModel extends BaseObservable
{
private int color;
public void setColor(int color)
{
this.color = color;
this.notifyPropertyChanged(BR.color);
}
@Bindable
public int getColor()
{
return this.color;
}
}
47. Two-Way data binding
public class TwoWayBindingActivity extends AppCompatActivity
{
private ActivityTwoWayBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_two_way);
binding.setViewModel(new TwoWayViewModel());
}
}
55. Dos
• From time to time look at generated code
• Learn from already implemented bindings for framework views
• Move view operations to custom bindings as much as possible
• Try to use it together with MVVM design pattern
• Give it a chance!
65. Method count
• ~700 methods from databinding library
• n methods from your custom binding adapters, conversions etc.
• k methods from generated code for each layout xml with data binding
enabled
66. Summary
Cons
Compiler errors are sometimes not saying
too much
Still in beta
Documentation is not updated
May break other libraries (for example squidb)
Pros
Easy to start
Less boilerplate code
Code generation during compilation
Easy to integrate with custom views and
libraries
Really powerful
Officialy created and supported by Google
Android Team