AAC:return 如何从 ViewModel 到 activity 结果(处理点击)?
AAC: How return result (handle click) from ViewModel to activity?
我想在我的项目中使用 Android 架构组件 (AAC)。
不错
这是我的 activity:
import androidx.appcompat.app.AppCompatActivity;
public class TradersActivity extends AppCompatActivity {
private TradersViewModel tradersViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tradersViewModel = ViewModelProviders.of(this).get(TradersViewModel.class);
tradersViewModel.getIsEnableSwipeProgress().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean isEnable) {
// do some work with UI
}
});
}
// button click
public void onClickViewJson(Trader trader) {
tradersViewModel.doClickJsonView(trader);
}
}
这是我的 ViewModel
public class TradersViewModel extends ViewModel {
private MutableLiveData<Boolean> isEnableSwipeProgress = new MutableLiveData<>();
public void doClickJsonView(Trader trader) {
// DO_SOME_COMPLEX_BUSINESS_LOGIC
}
public MutableLiveData<Boolean> getIsEnableSwipeProgress() {
return isEnableSwipeProgress;
}
}
在屏幕上我有按钮。当单击此按钮时,我调用 activity 的方法 - onClickViewJson(Trader trader)
.
这个方法调用tradersViewModel.doClickJsonView(trader);
在viewModel
这个方法中做一些复杂的业务逻辑。
方法完成后,我需要 return 结果 (json) 到我的 activity.
我该怎么做?
请记住,在 MVVM 中,ViewModel 并不知道您的视图。
您的 ViewModel 应该公开变量,以便您的视图可以观察它们并对其做出反应。
private MutableLiveData<Boolean> isEnableSwipeProgress = new MutableLiveData<>();
private MutableLiveData<JSONDto> jsonLiveData = new MutableLiveData<>();
public void doClickJsonView(Trader trader) {
// DO_SOME_COMPLEX_BUSINESS_LOGIC
jsonLiveData.postValue(/* the json you obtain after your logic finish */ )
}
public MutableLiveData<Boolean> getIsEnableSwipeProgress() {
return isEnableSwipeProgress;
}
public LiveData<JSONDto> getJsonDto() {
return this.jsonLiveData;
}
在您看来,您对 jsonDto
更改做出了反应:
tradersViewModel.getJsonDto().observe(this, new Observer<JSONDto>() {
@Override
public void onChanged(JSONDto json) {
if (json != null) {
// Do what you need here.
}
}
});
我想在我的项目中使用 Android 架构组件 (AAC)。 不错
这是我的 activity:
import androidx.appcompat.app.AppCompatActivity;
public class TradersActivity extends AppCompatActivity {
private TradersViewModel tradersViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tradersViewModel = ViewModelProviders.of(this).get(TradersViewModel.class);
tradersViewModel.getIsEnableSwipeProgress().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean isEnable) {
// do some work with UI
}
});
}
// button click
public void onClickViewJson(Trader trader) {
tradersViewModel.doClickJsonView(trader);
}
}
这是我的 ViewModel
public class TradersViewModel extends ViewModel {
private MutableLiveData<Boolean> isEnableSwipeProgress = new MutableLiveData<>();
public void doClickJsonView(Trader trader) {
// DO_SOME_COMPLEX_BUSINESS_LOGIC
}
public MutableLiveData<Boolean> getIsEnableSwipeProgress() {
return isEnableSwipeProgress;
}
}
在屏幕上我有按钮。当单击此按钮时,我调用 activity 的方法 - onClickViewJson(Trader trader)
.
这个方法调用tradersViewModel.doClickJsonView(trader);
在viewModel
这个方法中做一些复杂的业务逻辑。
方法完成后,我需要 return 结果 (json) 到我的 activity.
我该怎么做?
请记住,在 MVVM 中,ViewModel 并不知道您的视图。 您的 ViewModel 应该公开变量,以便您的视图可以观察它们并对其做出反应。
private MutableLiveData<Boolean> isEnableSwipeProgress = new MutableLiveData<>();
private MutableLiveData<JSONDto> jsonLiveData = new MutableLiveData<>();
public void doClickJsonView(Trader trader) {
// DO_SOME_COMPLEX_BUSINESS_LOGIC
jsonLiveData.postValue(/* the json you obtain after your logic finish */ )
}
public MutableLiveData<Boolean> getIsEnableSwipeProgress() {
return isEnableSwipeProgress;
}
public LiveData<JSONDto> getJsonDto() {
return this.jsonLiveData;
}
在您看来,您对 jsonDto
更改做出了反应:
tradersViewModel.getJsonDto().observe(this, new Observer<JSONDto>() {
@Override
public void onChanged(JSONDto json) {
if (json != null) {
// Do what you need here.
}
}
});