android-toggle icon indicating copy to clipboard operation
android-toggle copied to clipboard

Support for Data Binding Library

Open MihailsKuzmins opened this issue 6 years ago • 0 comments

I tried to use com.github.angads25.toggle.widget.LabeledSwitch with the Data Binding Library and I got the following error in the build: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Could not find event 'android:onAttrChanged' on View type 'com.github.angads25.toggle.widget.LabeledSwitch'

What I have tried is: <com.github.angads25.toggle.widget.LabeledSwitch android:layout_width="wrap_content" android:layout_height="wrap_content" app:textOn="Yes" app:textOff="No" android:checked="@={isChecked}"/>

or

<com.github.angads25.toggle.widget.LabeledSwitch android:layout_width="wrap_content" android:layout_height="wrap_content" app:textOn="Yes" app:textOff="No" app:on="@={isChecked}"/>

and my binding adapters look like this: @BindingAdapter("android:checked") // or "on" public static void setChecked(LabeledSwitch view, Boolean isChecked) { boolean blnValue = isChecked == null ? false : isChecked; view.setOn(blnValue); }

@InverseBindingAdapter(attribute = "android:checked") // or "on" public static Boolean isChecked(LabeledSwitch view) { return view.isOn(); }

What I had to do is 1) inherit from LabeledSwitch ; and 2) create this empty method: public void onAttrChanged(final InverseBindingListener attrChange) { }

Setting a listener in the method resulted in the behaviour that the switch was changed its value twice (e.g. false -> true -> false). Example: setOnClickListener(v -> setOn(!isOn));

MihailsKuzmins avatar Apr 24 '19 20:04 MihailsKuzmins