I been trying to control the visibility of a view using the Implicit Attribute Listeners(reference) in android data binding which allows to access views by id and access at
Step 1: create BindingAdapter:
@BindingAdapter("android:visibility")
public static void setVisibility(final View view, @IdRes int layourId) {
SwitchCompat switcher = (SwitchCompat)view.getRootView().findViewById(layourId)
switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
view.setVisibility(isChecked ? View.VISIBLE : View.GONE);
}
}
}
Step 2: import R
class in databinding data section at you layout.xml:
Step 3: bind custom view to your switcher like this: