Android 2 data binding example does not work as described

I read this article on data binding using two methods of binding to Android files.

I noticed that the code is a bit vague and decided to implement a working example and put it on github, so it will be much easier for other guys to dive into it. But, following the instructions in this article, I could not get it to work.

In my example, I have a main action with a switch and a user control, as well as a switch. Therefore, when I check the main switch, it updates everything correctly and works as expected, but when I check / uncheck the internal switch, it does not affect the main viewing scheme and anything in action - therefore, two-way binding does not work .

Please help me find the reason why this is happening and fix the problem.


The code is fixed and now works as expected in Android Studio 2.2 beta 1 at least.

Link to sample code on github

+4
source share
1 answer

. CustomSwitcher , . onValChanged.

:

public CustomSwitcher(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.binding = CustomSwitcherBinding.inflate(LayoutInflater.from(context), this, true);
}

public void setVm(boolean vmVal){
    this.vm = vmVal;
    this.binding.setItem(vm);
}

, . . , , , .

public CustomSwitcher(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.binding = CustomSwitcherBinding.inflate(LayoutInflater.from(context), this, true);
    this.binding.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            if (propertyId == BR.item) {
                setVm(binding.getItem());
            }
        }
    });
}

public void setVm(boolean vmVal){
    if (vmVal != this.vm) {
        this.vm = vmVal;
        this.binding.setItem(vm);
        if (this.onValChanged != null) {
            this.onValChanged.onValChanged(this, vmVal);
        }
    }
}
+2

Source: https://habr.com/ru/post/1650974/


All Articles