我有n个EditText,它们有自己的文本:
编辑文本1->文本:13
编辑文本2->文本:15
编辑文本3->文本:20
还有一个API方法被命名为getNewValue(String currentValue)
。由于当前值,它将获得每个EditText的新值。
场景:如果每个EditText都成为焦点,则会发送getNewValue()
请求,并且会在焦点EditText上设置新值。
如何使用Reform、RxJava和ViewModel实现此目标
我试过这个代码:
片段:
editText1.setOnFocusChangeListener(getEtFocusChangeListener(editText1.getText().toString()));
editText2.setOnFocusChangeListener(getEtFocusChangeListener(editText2.getText().toString()));
editText3.setOnFocusChangeListener(getEtFocusChangeListener(editText3.getText().toString()));
private View.OnFocusChangeListener getEtFocusChangeListener(String currentValue) {
return (view, hasFocus) -> {
if (hasFocus) {
EditText et = (EditText) v;
viewModel.getNewValue(currentValue);
viewModel.getNewValueResponse().observe(getViewLifecycleOwner(), newValue -> et.setText(newValue));
viewModel.getNewValueError().observe(getViewLifecycleOwner(), throwable -> Log.i(TAG, "New Value Error -----> " + throwable));
}
};
}
视图模型:
private final MutableLiveData<String> newValueResponse = new MutableLiveData<>();
private final MutableLiveData<Throwable> newValueError = new MutableLiveData<>();
public void getNewValue(String currentValue) {
apiService.getNewValue(currentValue)
.subscribeOn(Schedulers.io())
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(@NonNull String newValue) {
newValueResponse.postValue(newValue);
}
@Override
public void onError(@NonNull Throwable throwable) {
newValueError.postValue(throwable);
}
});
}
public LiveData<String> getNewValueResponse() {
return newValueResponse;
}
public LiveData<Throwable> getNewValueError() {
return newValueError;
}
ApiService:
@GET("Values/GetNewValue")
Single<String> getNewValue(@Query("currentValue") String currentValue);
一种解决方案是声明一个全局变量来保存最后一个集中的EditText
,并在请求响应时使用它,但我认为有更好的&更智能的解决方案。
我对Android编程不是很熟悉,如果我遗漏了什么并犯了任何错误,我很抱歉,但你为什么不把对EditText
的引用传递给getNewValue
方法呢?
例如:
private final MutableLiveData<Pair<EditText, String>> newValueResponse = new MutableLiveData<>();
private final MutableLiveData<air<EditText, Throwable>> newValueError = new MutableLiveData<>();
//...
public void getNewValue(final EditText editText, final String currentValue) {
apiService.getNewValue(currentValue)
.subscribeOn(Schedulers.io())
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(@NonNull String newValue) {
newValueResponse.postValue(Pair.create(editText, newValue));
}
@Override
public void onError(@NonNull Throwable throwable) {
newValueError.postValue(Pair.create(editText, throwable));
}
});
}
public LiveData<Pair<EditText, String>> getNewValueResponse() {
return newValueResponse;
}
public LiveData<Pair<EditText, Throwable>> getNewValueError() {
return newValueError;
}
OnFocusChangeListener
将如下所示:
private View.OnFocusChangeListener getEtFocusChangeListener(String currentValue) {
return (view, hasFocus) -> {
if (hasFocus) {
EditText et = (EditText) v;
viewModel.getNewValue(et, currentValue);
viewModel.getNewValueResponse().observe(getViewLifecycleOwner(), newValue -> newValue.first.setText(newValue.second));
viewModel.getNewValueError().observe(getViewLifecycleOwner(), throwable -> Log.i(TAG, "New Value Error -----> " + throwable.second));
}
};
}
在某种程度上,我发现最后一个代码片段很奇怪,因为如果你仔细考虑它,由于API调用的异步性质,实际结果可能是不同的EditText
。也许我可以用这样的东西来缓解这个问题:
private View.OnFocusChangeListener getEtFocusChangeListener(String currentValue) {
return (view, hasFocus) -> {
if (hasFocus) {
EditText et = (EditText) v;
viewModel.getNewValue(et, currentValue);
viewModel.getNewValueResponse().observe(getViewLifecycleOwner(), newValue -> {
// Only perform the operation if the `EditText` is the affected one
if (et.equals(newValue.first)) {
newValue.first.setText(newValue.second);
}
});
viewModel.getNewValueError().observe(getViewLifecycleOwner(), throwable -> {
if (et.equals(throwable.first)) {
Log.i(TAG, "New Value Error -----> " + throwable.second);
}
});
}
};
}
在我看来,根据用例,您可以去掉newValueResponse
和newValueError
MutableLiveData
实例。例如:
public void getNewValue(final EditText editText, final String currentValue) {
apiService.getNewValue(currentValue)
.subscribeOn(Schedulers.io())
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(@NonNull String newValue) {
editText.seText(newValue);
}
@Override
public void onError(@NonNull Throwable throwable) {
Log.i(TAG, "New Value Error -----> " + throwable);
}
});
}
和:
private View.OnFocusChangeListener getEtFocusChangeListener(String currentValue) {
return (view, hasFocus) -> {
if (hasFocus) {
EditText et = (EditText) v;
viewModel.getNewValue(et, currentValue);
}
};
}
或者更具语义:
public void getNewValue(final String currentValue) {
return apiService.getNewValue(currentValue)
.subscribeOn(Schedulers.io());
}
private View.OnFocusChangeListener getEtFocusChangeListener(String currentValue) {
return (view, hasFocus) -> {
if (hasFocus) {
EditText et = (EditText) v;
viewModel.getNewValue(et, currentValue)
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(@NonNull String newValue) {
et.seText(newValue);
}
@Override
public void onError(@NonNull Throwable throwable) {
Log.i(TAG, "New Value Error -----> " + throwable);
}
});
}
};
}