最近google推出JetPack官方架構組件,最近有時間在網上看了不少相似的文章,對ViewModel和lifeCycle有了大概的瞭解,谷歌意在使用這些組件完成代碼解耦,實現數據驅動UI的模式,可是尚未深刻的瞭解,效率優先,仍是動手才能看到這些架構組件具體是怎樣的使用,來實現簡單的MVVM模式。 每一個組件的單獨使用網上有不少文章,你們能夠自行去查看,我也是拿來主義,直接上代碼。java
接口使用的網上一位大神測試用的接口,UI也是直接使用,本人較懶,請求使用流行的Retrofit2+rxjava2,將其封裝在BaseModel中:git
public class BaseModel {
public BaseModel() {}
public void getImage(String format, int idx, int n, BaseObserver<ImageBean> observer) {
ApiRepertory.getInstance().getService().getImage(format, idx, n)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
複製代碼
此處的Presenter用於將ViewModel中的業務綁定到不一樣的生命週期中去執行業務邏輯。github
public abstract class BaseLifeCycle<P extends BasePresenter> implements ILifeCycle {
protected Context mContext;
protected P mPresenter;
public BaseLifeCycle(Context context) {
this.mContext = context;
}
protected void addPresenter(P mPresenter) {
if (mPresenter != null) {
this.mPresenter = mPresenter;
}
}
@Override
public void onCreate(@Nullable LifecycleOwner owner) {
}
@Override
public void onDestroy(@Nullable LifecycleOwner owner) {
}
@Override
public void onStart(@Nullable LifecycleOwner owner) {
}
@Override
public void onResume(@Nullable LifecycleOwner owner) {
}
@Override
public void onPause(@Nullable LifecycleOwner owner) {
}
@Override
public void onStop(@Nullable LifecycleOwner owner) {
}
@Override
public void onLifecycleChanged(@Nullable LifecycleOwner owner, @Nullable Lifecycle.Event event) {
}
}
複製代碼
這裏ILifeCycle接口用來繼承LifecycleObserver,SupportActivity內部已經實現了LifecycleOwner接口用來獲取lifeCycle,加入LifecycleObserver來添加觀察者來響應不一樣的生命週期。設計模式
public interface ILifeCycle extends LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void onCreate(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void onStart(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void onResume(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void onPause(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void onStop(@Nullable LifecycleOwner owner);
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onLifecycleChanged(@Nullable LifecycleOwner owner,
@Nullable Lifecycle.Event event);
}
複製代碼
還有此處的BasePresenter用來綁定ViewModel,實現其中的業務邏輯。bash
public abstract class BasePresenter<M extends ViewModel> {
protected M mViewModel;
public BasePresenter(M viewModel) {
if (viewModel != null) {
this.mViewModel = viewModel;
}
}
}
複製代碼
下面是將lifeCycle和databinding寫入BaseActivity中比較簡單,沒有集成過多的其餘組件。 getLifecycle().addObserver(mLifeCycle);將不一樣頁面的生命週期邏輯綁定到不一樣頁面中, mLifeCycle.addPresenter(mPresenter);將業務處理部分加入到生面週期中,用於在不一樣的生命週期中處理不一樣的業務。 startListenerData();此處用於實現ViewModel數據返回View進行響應刷新的邏輯。網絡
public abstract class BaseActivity<L extends BaseLifeCycle, V extends ViewDataBinding, P extends BasePresenter>
extends AppCompatActivity{
protected Context mContext;
protected L mLifeCycle;
protected V dataBinding;
protected P mPresenter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
dataBinding = DataBindingUtil.setContentView(this, getLayoutRes());
initView();
getLifecycle().addObserver(mLifeCycle);
mLifeCycle.addPresenter(mPresenter);
startListenerData();
}
protected abstract void startListenerData();
protected abstract void initView();
protected abstract int getLayoutRes();
}
複製代碼
下面看下在Activity中的具體實現: ImageViewModel中就是具體的業務邏輯的實現與數據的綁定; MainLifeCycle是該頁面的生命週期,presenter在不一樣的生命週期實現不一樣的處理。架構
public class MainActivity extends BaseActivity<MainLifeCycle, ActivityMainBinding, ImagePresenter> {
private ProgressDialog mProgressDialog;
private ImageViewModel baseViewModel;
@Override
protected void initView() {
mLifeCycle = new MainLifeCycle(this);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("加載中");
baseViewModel = ViewModelProviders.of(this).get(ImageViewModel.class);
dataBinding.setClick(new ClickHandler());
//將presener綁定生命週期
mPresenter = new ImagePresenter(baseViewModel);
mProgressDialog.show();
}
@Override
protected int getLayoutRes() {
return R.layout.activity_main;
}
@Override
protected void startListenerData() {
baseViewModel.getMutableLiveData().observe(this, new Observer<Data<ImageBean.ImagesBean>>() {
@Override
public void onChanged(@Nullable Data<ImageBean.ImagesBean> imagesBeanData) {
if (imagesBeanData.getErrorMsg() != null) {
Toast.makeText(mContext, imagesBeanData.getErrorMsg(), Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
return;
}
dataBinding.setImageBean(imagesBeanData.getData());
setTitle(imagesBeanData.getData().getCopyright());
mProgressDialog.dismiss();
}
});
}
public class MainLifeCycle extends BaseLifeCycle<ImagePresenter> {
public MainLifeCycle(Context context) {
super(context);
}
@Override
public void onStart(@Nullable LifecycleOwner owner) {
Log.e("MainActivity", "onCreate");
mPresenter.loadImage();
}
}
複製代碼
好了,簡單的mvvm模式大概就是這樣,本人能力有限,有不少不足之處,若有大神請指摘,下面貼出代碼的GitHub地址:github.com/ale0201/mvv…mvvm