How to pass ObservableField inside internal control

The main view model is as follows:

class MainVM{
    public ObservableField<String> title;
    public ObservableField<Boolean> isFlexible;
}

The main layout is as follows:

<layout>
  <date><variable name="item" type="MainVM"></data>
  <LinearLayout>
     <TextView text="@{item.title}"/>
     <CustomCtrl1 vm="@{item.isFlexible}">
  </LinearLayout>
</layout>

and the CustomCtrllayout looks somewhat like

   <layout>
      <date><variable name="item" type="boolean"></data>
      <LinearLayout>
          ...
         <Switch checked="@{item}"/>
          ...
      </LinearLayout>
    </layout>

The problem is that ObservableFieldfrom is MainVMconverted to a boolean when passed to CustomCtrl, and after that changing the boolean inside CustomCtrldoes not affect MainVM. The first idea was to change the CustomCtrl'sviewmodel from Booleanto ObservableField<Boolean>, but for some reason this is not allowed.

So the question is what is the correct way to skip ObservableFieldinside internal controls.

+4
1

- . Android Studio 2.1 . Android Studio 2.2 , . include , :

<layout>
  <date><variable name="item" type="MainVM"></data>
  <LinearLayout>
     <TextView android:text="@{item.title}"/>
     <include layout="@layout/other" app:vm="@={item.isFlexible}">
  </LinearLayout>
</layout>

:

<layout>
  <date><variable name="item" type="boolean"></data>
  <LinearLayout>
      ...
     <switch android:checked="@={item}"/>
      ...
  </LinearLayout>
</layout>

. , . , ( , ):

@InverseBindingMethods({
     InverseBindingMethod(type = CustomControl.class, attribute="vm")})
public class CustomCtrl extends View {
    private CustomCtrlBinding binding;
    private InverseBindingAdapter listener;

    public CustomCtrl(...) {
        binding = ...
        binding.addOnPropertyChangedCallback(new OnPropertyChangedCallback() {
            @Overriide
            public void OnPropertyChanged(Observable sender, int propertyId) {
                if (listener != null) {
                    listener.onChange();
                }
            }
        });
    }

    @Bindable
    public boolean getVm() { return binding.getItem(); }

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

    @BindingAdapter("vmAttrChanged")
    public static void setListener(CustomCtrl view,
            InverseBindingListener listener) {
        view.listener = listener;
    }
}

:

<layout>
  <date><variable name="item" type="MainVM"></data>
  <LinearLayout>
     <TextView android:text="@{item.title}"/>
     <CustomCtrl app:vm="@={item.isFlexible}">
  </LinearLayout>
</layout>

:

<layout>
  <date><variable name="item" type="boolean"></data>
  <LinearLayout>
      ...
     <switch android:checked="@={item}"/>
      ...
  </LinearLayout>
</layout>

, , InverseBindingListener .

+1

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


All Articles