对象字段更改时 LiveData 更新值

LiveData update values when field of object change

今天我正在尝试使用 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) 更新时(在执行计算时)更新我的视图。

我该怎么做?

试试这个:

您需要使您的模型 class 扩展 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>