SelfZhangTQ / T-MVVM

This repository contains a detailed sample app that implements MVVM architecture using LiveData,ViewModel,Retrofit,Rxjava
685 stars 163 forks source link

如果我想在页面中加上网络请求时等待的弹窗,是否有较好的办法 #19

Open Destroyer716 opened 5 years ago

Destroyer716 commented 5 years ago

比如在网络请求开始前显示loading 弹窗,网络请求结束时销毁弹窗,ViewModel里面调用Model层获取数据,那么在调用Model层前显示loading,请求结束时销毁loading,而ViewModel不建议应用View层,我该如何通知View层去显示或者销毁loading弹窗呢,暂时我想到的办法是另外每个页面都需要单独定义一个LiveData去设置显示还是销毁,然后View层观察他的值的变化。

想问下有没有更好的办法。

xchengDroid commented 5 years ago

在ViewModel 里面定义一个public 的 loadingLiveData.,开始和结束发送数据,然后在View层去添加observer

xchengDroid commented 5 years ago

如 LiveData loadingLiveData=new LiveData<>(); loadingLiveData.setValue(true|false)