当对象字段更改时,LiveData会更新值

时间:2019-07-08 21:01:50

标签: android-databinding android-livedata android-mvvm mutablelivedata

今天,我正在尝试使用LiveData(MutableLiveData)对象具有一些动态值。 (以MVVM模式显示)

我使用了这样的模型对象:

public class Object {
    private String name;
    private float internalvalue;
    private float in1;
    private float out1;
    private float out2;

    public Object(String name, float internalvalue){
        this.name = name;
        this.internalvalue = internalvalue;
    }

    public float getOut1(){
        return this.out1;
    }

    public float getOut2(){
        return this.out2;
    }

    public void setIn1(float in1){
        this.in1 = in1;

    }

    private void performSomething(float internalvalue, float in1){
        SubClassSingleton.performSomething(internalvalue, in1, new SubClassSingletonListener() {
            @Override
            public void onSuccess(float out1, float out2){
                this.out1 = out1;
                this.out2 = out2;
            }
        });
    }
}

我使用这样的ViewModel:

public class MainViewModel {
    public MutableLiveData<Object> obj;

    public MainViewModel(){
        this.obj = new MutableLiveData<>();
        this.obj.postValue(new Object("Name", 50.0f);
    }
}

在MainFragment中:

public class MainFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        MainFragmentBinding binding = MainFragmentBinding.inflate(getLayoutInflater());
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }
}

在视图中:

<layout ...
    <data>
        <variable
            name="viewModel"
            type="app.example.MainViewModel"/>
    </data>
    <LinearLayout
        ...>
        <EditText
            android:text="@={viewModel.obj.in1}"
            .../>
        <TextView
            android:text="@{viewModel.obj.out1}"
            .../>
        <TextView
            android:text="@{viewModel.obj.out2}"
            .../>
    </LinearLayout>
</layout>

我想在模型的值(out1,out2)更新时(执行计算时)更新视图。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试一下:

您需要使您的模型类扩展BaseObservable。

public class Object extends BaseObservable {
    private String name;
    private float internalvalue;
    private float in1;
    private float out1;
    private float out2;

    public Object(String name, float internalvalue) {
        this.name = name;
        this.internalvalue = internalvalue;
    }

    public float getOut1() {
        return this.out1;
    }

    public void setOut1(float out1) {
        this.out1 = out1;
        notifyChange();
    }

    public float getOut2() {
        return this.out2;
    }

    public void setOut2(float out2) {
        this.out2 = out2;
        notifyChange();
    }

    public void setIn1(float in1) {
        this.in1 = in1;
    }

    private void performSomething(float internalvalue, float in1) {
        SubClassSingleton.performSomething(internalvalue, in1, new SubClassSingletonListener() {
            @Override
            public void onSuccess(float out1, float out2) {
                setOut1(out1);
                setOut2(out2);
            }
        });
    }
}

在View中,您不能将float直接绑定到view。它必须是字符串,所以像这样绑定:

<layout ...
    <data>
        <variable
            name="viewModel"
            type="app.example.MainViewModel"/>
    </data>
    <LinearLayout
        ...>
        <EditText
            android:text='@={""+viewModel.obj.in1}'
            .../>
        <TextView
            android:text='@{""+viewModel.obj.out1}'
            .../>
        <TextView
            android:text='@{""+viewModel.obj.out2}'
            .../>
    </LinearLayout>
</layout>