说到MVVM,不得不提MVC、MVP,前面的那对CP,在某些角度或理解上,倒挺符合他们CP的气质,毕竟他们真的太相似了,写完这三篇文章之后,会专门说说开发的时候该如何去选用这些框架。
言归正传,MVVM新引入了VM的概念,也就是ViewModel,用的时候可以继承ViewModel或者AndroidViewModel,谈到这里,不禁要想,这才是google的亲儿子吗,试问,用MVC的时候Android提供了Controller吗?MVP时android提供了Presenter吗?写到这里的时候,小编马上就去搜索了下,还真有Presenter,我是瑟瑟发抖,真准备求android 粑粑原谅,看了看源码,不禁把肾放回了腰间。androidx.leanback.widget.Presenter 源码有点长就不贴了,先看看示例
public class StringTextViewPresenter extends Presenter {
// This class does not need a custom ViewHolder, since it does not use
// a complex layout.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
return new ViewHolder(new TextView(parent.getContext()));
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
String str = (String) item;
TextView textView = (TextView) viewHolder.mView;
textView.setText(item);
}
@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
// Nothing to unbind for TextView, but if this viewHolder had
// allocated bitmaps, they can be released here.
}
}
从例子上看,类似于在Adapter中的做法,虽然如此也能实现MVP的效果,并且这种做法会使View变得更干净,但是这个时候除了xml,还有V的存在吗?貌似这是最理想的状态,氮素啊,这不又回到了Activity的状态了。所以,我姑且就觉得这不是为MVP准备的。
更何况还有DataBinding,MVVM简直如有神助。
MVC:(View-Model-Controller)
将View、Model、Controller代码块进行划分,使得程序大部分分离,降低耦合。这个模块,后续会出一个专门的文章,之所以放在最后,文章中会提到。
【MVP:(VIew-Model-Presenter)
Presenter整个功能的服务者,当用户通过界面(View)与应用进行交互或潜在交互时,界面将有效事件上报给Presenter,Presenter则根据事件需求进行处理,包括与数据(Model)之间的交互。形象来说就是:MVP就是一个非自助式餐厅,顾客就相当于我们的用户,餐厅本身相当于View,餐厅工作人员相当于Presenter,餐厅的食材相当于Model。用户与View进行交互时,相当于客户来到餐厅,此时会触发工作人员进行服务,有点餐操作时,Presenter获取Model数据,并呈现给用户。
MVVM:(Model–View–ViewModel)
MVVM可以算是MVP的升级版,将Presenter改名为ViewModel。关键在于View和Model的双向绑定,当View有用户输入后,ViewModel通知Model更新数据,同理Model数据更新后,ViewModel通知View更新。
1、Android官方架构组件Lifecycle
2、Android官方架构组件ViewModel
3、Android官方架构组件LiveData
4、Android官方架构组件Paging
5、Android官方架构组件Navigation
6、Android官方架构组件Data
7、Android-common
8、Android-common-mvvm
package com.ww7h.common.mvvm.v;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import com.ww7h.ww.common.bases.activity.BaseActivity;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: Android-common-mvvm
* 包名: com.ww7h.common.mvvm.v
* 创建时间: 2019/4/5 18:54
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public abstract class BaseVActivity, B extends ViewDataBinding> extends BaseActivity {
protected B vdBinding;
@Override
protected boolean getDesignPattern() {
return true;
}
@Override
protected void initContentView() {
super.initContentView();
vdBinding = DataBindingUtil.setContentView(this, getContentView());
}
}
package com.ww7h.common.mvvm.v;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ww7h.ww.common.bases.fragment.BaseFragment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: Android-common-mvvm
* 包名: com.ww7h.common.mvvm.v
* 创建时间: 2019/4/5 18:57
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public abstract class BaseVFragment, B extends ViewDataBinding> extends BaseFragment {
/**
* 当前视图绑定的对象
*/
protected B vdBinding;
@Nullable
@Override
protected View getContentView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container) {
vdBinding = DataBindingUtil.inflate(inflater, getResourceId() ,container , false);
return vdBinding.getRoot();
}
@Override
protected boolean getDesignPattern() {
return true;
}
}
package com.ww7h.purchasing.main.view;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.support.annotation.Nullable;
import com.ww7h.common.mvvm.v.BaseVActivity;
import com.ww7h.purchasing.PTApplication;
import com.ww7h.purchasing.R;
import com.ww7h.purchasing.databinding.ActivityMainDetailBinding;
import com.ww7h.purchasing.main.event.MainDetailEvent;
import com.ww7h.purchasing.main.model.MainDetailModel;
import com.ww7h.purchasing.main.viewmodel.MainDetailViewModel;
import com.ww7h.ww.common.utils.DensityUtil;
import com.ww7h.ww.common.utils.LogUtil;
import com.ww7h.ww.common.utils.ScreenUtil;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/23 20:53
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailActivity extends BaseVActivity {
MainDetailViewModel mViewModel;
@Override
protected int getContentView() {
return R.layout.activity_main_detail;
}
@Override
protected void initAction() {
vdBinding.setEvent(new MainDetailEvent(mViewModel));
}
@Override
protected void initView() {
MainDetailViewModel.Factory factory = new MainDetailViewModel.Factory(PTApplication.getInstance());
mViewModel = ViewModelProviders.of(this, factory).get(MainDetailViewModel.class);
subscribeUi(mViewModel.getMainDetailModel());
}
private MainDetailModel mainDetailModel;
private void subscribeUi(LiveData modelLiveData) {
modelLiveData.observe(this, new Observer() {
@Override
public void onChanged(@Nullable MainDetailModel mainModel) {
if (mainModel != null) {
float width = ScreenUtil.Companion.getScreenWidth(activity) / 8f;
mainDetailModel = mainModel;
vdBinding.setMainDetailModel(mainDetailModel);
vdBinding.bNav1.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.bNav2.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 20));
vdBinding.bNav3.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.bNav3.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav3.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 25));
vdBinding.bNav4.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 15));
}
vdBinding.executePendingBindings();
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtil.e(TAG, "-------");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "-------");
}
}
package com.ww7h.purchasing.main.model;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import com.ww7h.common.mvvm.BR;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.model
* 创建时间: 2019/4/25 20:55
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailModel extends BaseObservable {
private String navi1Name;
private String navi2Name;
private String navi3Name;
private String navi4Name;
private int navi1Number;
private int navi2Number;
private int navi3Number;
private int navi4Number;
@Bindable
public String getNavi1Name() {
return navi1Name;
}
@Bindable
public int getNavi1Number() {
return navi1Number;
}
@Bindable
public String getNavi2Name() {
return navi2Name;
}
@Bindable
public int getNavi2Number() {
return navi2Number;
}
@Bindable
public String getNavi3Name() {
return navi3Name;
}
@Bindable
public int getNavi3Number() {
return navi3Number;
}
@Bindable
public String getNavi4Name() {
return navi4Name;
}
@Bindable
public int getNavi4Number() {
return navi4Number;
}
public void setNavi1Name(String navi1Name) {
this.navi1Name = navi1Name;
}
public void setNavi2Name(String navi2Name) {
this.navi2Name = navi2Name;
}
public void setNavi3Name(String navi3Name) {
this.navi3Name = navi3Name;
}
public void setNavi4Name(String navi4Name) {
this.navi4Name = navi4Name;
}
public void setNavi1Number(int navi1Number) {
this.navi1Number = navi1Number;
notifyPropertyChanged(BR.navi1Number);
}
public void setNavi2Number(int navi2Number) {
this.navi2Number = navi2Number;
}
public void setNavi3Number(int navi3Number) {
this.navi3Number = navi3Number;
}
public void setNavi4Number(int navi4Number) {
this.navi4Number = navi4Number;
}
}
package com.ww7h.purchasing.main.viewmodel;
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.support.annotation.NonNull;
import com.ww7h.common.mvvm.vm.BaseVM;
import com.ww7h.purchasing.main.model.MainDetailModel;
import org.jetbrains.annotations.NotNull;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.viewmodel
* 创建时间: 2019/4/26 10:25
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailViewModel extends BaseVM {
private MainDetailModel mainDetailModel;
private MainDetailViewModel(@NonNull Application application) {
super(application);
}
public LiveData getMainDetailModel() {
mainDetailModel = new MainDetailModel() {
{
setNavi1Name("清单");
setNavi1Number(2);
setNavi1Name("日程");
setNavi2Number(88);
setNavi1Name("购买人");
setNavi3Number(111);
setNavi1Name("邮寄");
setNavi4Number(1);
}
};
return new MutableLiveData() {
{
postValue(mainDetailModel);
}
};
}
public void updateNavi1Number(int number) {
mainDetailModel.setNavi1Number(number);
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
@NonNull
private final Application mApplication;
public Factory(@NonNull Application application) {
mApplication = application;
}
@NotNull
@Override
public T create(@NotNull Class modelClass) {
return (T) new MainDetailViewModel(mApplication);
}
}
}
package com.ww7h.purchasing.main.event;
import android.view.View;
import com.ww7h.purchasing.main.viewmodel.MainDetailViewModel;
import com.ww7h.ww.common.utils.ToastUtil;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/26 11:40
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailEvent {
private MainDetailViewModel viewModel;
public MainDetailEvent(MainDetailViewModel viewModel) {
this.viewModel = viewModel;
}
public void jumpClick(View view) {
ToastUtil.showShortToast("123");
viewModel.updateNavi1Number(1);
}
}
package com.ww7h.purchasing.main.view;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import com.ww7h.common.mvvm.v.BaseVFragment;
import com.ww7h.purchasing.PTApplication;
import com.ww7h.purchasing.R;
import com.ww7h.purchasing.databinding.FragmentMainBinding;
import com.ww7h.purchasing.main.model.MainModelInterface;
import com.ww7h.purchasing.main.view.adapter.MainAdapter;
import com.ww7h.purchasing.main.viewmodel.MainViewModel;
import com.ww7h.ww.common.bases.view.recyclerview.decoration.SpaceItemDecoration;
import com.ww7h.ww.common.listeners.OnRecyclerItemClick;
import com.ww7h.ww.common.utils.DensityUtil;
import java.util.List;
import java.util.Objects;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing
* 创建时间: 2019/4/17 14:51
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainFragment extends BaseVFragment implements OnRecyclerItemClick {
private MainAdapter mainAdapter;
@Override
public int getResourceId() {
return R.layout.fragment_main;
}
@Override
public void initAction() {
}
@Override
public void initView() {
mainAdapter = new MainAdapter(getActivity());
mainAdapter.setRecyclerItemClick(this);
vdBinding.mainContentRv.setLayoutManager(new LinearLayoutManager(getActivity()));
vdBinding.setMainAdapter(mainAdapter);
vdBinding.mainContentRv.addItemDecoration(new SpaceItemDecoration(DensityUtil.INSTANCE.dp2px(Objects.requireNonNull(getActivity()),1f),1));
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MainViewModel.Factory factory = new MainViewModel.Factory(PTApplication.getInstance());
final MainViewModel viewModel = ViewModelProviders.of(this, factory).get(MainViewModel.class);
subscribeUi(viewModel.getMainModelList());
}
private void subscribeUi(LiveData> liveData) {
liveData.observe(this, new Observer>() {
@Override
public void onChanged(@Nullable List mainModels) {
if (mainModels != null) {
mainAdapter.replaceDataList(mainModels);
}
vdBinding.executePendingBindings();
}
});
}
@Override
public void onItemClick(@org.jetbrains.annotations.Nullable MainModelInterface mainModelInterface) {
assert mainModelInterface != null;
Intent intent = new Intent(getActivity(), MainDetailActivity.class);
startActivity(intent);
}
}
package com.ww7h.purchasing.main.view.adapter;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.ww7h.purchasing.R;
import com.ww7h.purchasing.databinding.ItemMainBinding;
import com.ww7h.purchasing.main.model.MainModelInterface;
import com.ww7h.ww.common.bases.view.recyclerview.adapters.BaseRecyclerViewAdapter;
import com.ww7h.ww.common.bases.view.recyclerview.adapters.RecyclerViewHolder;
import com.ww7h.ww.common.listeners.OnRecyclerItemClick;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view.adapter
* 创建时间: 2019/4/17 16:51
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainAdapter extends BaseRecyclerViewAdapter {
private OnRecyclerItemClick recyclerItemClick;
private Context mContext;
public MainAdapter(Context context) {
mContext = context;
}
@Override
protected void onBindViewHolder(MainViewHolder holder, int position, int viewType) {
holder.binding.setOnItemClickListener(recyclerItemClick);
holder.binding.setItemModel(getItem(position));
}
@Override
protected boolean areItemsTheSame(MainModelInterface oldM, MainModelInterface newM) {
return oldM.getItemName().equals(newM.getItemName());
}
@Override
protected boolean areContentsTheSame(MainModelInterface oldM, MainModelInterface newM) {
return oldM.getItemName().equals(newM.getItemName())
&& oldM.getItemContent().equals(newM.getItemContent())
&& oldM.getItemIconPath().equals(newM.getItemIconPath());
}
@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ItemMainBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_main,
viewGroup, false);
return new MainViewHolder(binding);
}
public void setRecyclerItemClick(OnRecyclerItemClick recyclerItemClick) {
this.recyclerItemClick = recyclerItemClick;
}
class MainViewHolder extends RecyclerViewHolder {
ItemMainBinding binding;
MainViewHolder(ItemMainBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
package com.ww7h.common.mvvm.v.adapter;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: Android-common-mvvm
* 包名: com.ww7h.common.mvvm.v.adapter
* 创建时间: 2019/4/26 16:18
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class WidgetBindingAdapter {
@BindingAdapter({"image"})
public static void glideLoadImage(ImageView imageView, String url) {
Glide.with(imageView.getContext()).load(url).into(imageView);
}
}