当前比较流行的框架的 MVP + Retrofit + OkHttp + Dagger2 + butterknife全家桶,今天略做总结。
MVP由原来的MVC演化而来,主要解决了以前MVC由于杂糅导致Activity、Fragment臃肿的问题,使得项目逻辑更加清楚。但使用的时候根据自己的需要,也不必需要过分拘谨于MVP的形式。
下面是我对MVP的理解,从而进行的Base类封装。
BaseView
public interface BaseView {
/**
* 显示加载框
*/
void showLoading();
/**
* 隐藏加载框
*/
void hideLoading();
/**
* Toast
* @param toastStr
*/
void showToast(String toastStr);
/**
* 跳转Activity
* @param activity
*/
void goPage(Class<? extends Activity> activity);
/**
* 跳转Activity
* @param activity
* @param bundle
*/
void goPage(Class<? extends Activity> activity, Bundle bundle);
/**
* 跳转Activity
* @param activity
* @param bundle
* @param requestCode
*/
void goPage(Class<? extends Activity> activity, Bundle bundle, int requestCode);
}
BasePresenter
public interface BasePresenter<T extends BaseView> {
/**
* 绑定View
* @param view
*/
void attachView(T view);
/**
* 解除绑定
*/
void detachView();
}
BasePAV (Presenter与View的关联类)
public class BasePAV<T extends BaseView> implements BasePresenter<T> {
protected T mView;
@Override
public void attachView(T view) {
mView = view;
}
@Override
public void detachView() {
if (mView != null) {
mView = null;
}
}
}
BaseActivity
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
@Inject
@Nullable
public T mPresenter;
protected KProgressHUD mKProgressHUD;
protected Unbinder unbinder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
unbinder = ButterKnife.bind(this);
initInject();
if (mPresenter != null) {
mPresenter.attachView(this);
}
initViewAndData();
}
protected ActivityComponent getActivityComponent() {
return DaggerActivityComponent.builder()
.appComponent(App.getAppComponent())
.activityModule(new ActivityModule(this))
.build();
}
protected abstract int getLayout();
protected abstract void initInject();
protected abstract void initViewAndData();
@Override
public void goPage(Class<? extends Activity> activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
}
@Override
public void goPage(Class<? extends Activity> activity, Bundle bundle) {
Intent intent = new Intent(this, activity);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
@Override
public void goPage(Class<? extends Activity> activity, Bundle bundle, int requestCode) {
Intent intent = new Intent(this, activity);
intent.putExtra("bundle", bundle);
startActivityForResult(intent, requestCode);
}
@Override
public void showLoading() {
mKProgressHUD = KProgressHUD.create(this);
mKProgressHUD.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setCancellable(true)
.setAnimationSpeed(2)
.setDimAmount(0.5f)
.show();
}
@Override
public void hideLoading() {
if (mKProgressHUD != null) {
mKProgressHUD.dismiss();
}
}
@Override
public void showToast(String toastStr) {
MyToast.showToast(this, toastStr);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
unbinder.unbind();
}
}
BaseFragment
public abstract class BaseFragment<T extends BasePresenter> extends Fragment implements BaseView {
@Inject
@Nullable
protected T mPresenter;
protected Unbinder unbinder;
protected View mRootView;
protected KProgressHUD mKProgressHUD;
protected abstract int getLayoutId();
protected abstract void initInject();
protected abstract void initEventAndData();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = inflater.inflate(getLayoutId(), container, false);
}
unbinder = ButterKnife.bind(this, mRootView);
initInject();
return mRootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mPresenter.attachView(this);
unbinder = ButterKnife.bind(this, view);
initEventAndData();
}
protected FragmentComponent getFragmentComponent() {
return DaggerFragmentComponent.builder()
.appComponent(App.getAppComponent())
.fragmentModule(new FragmentModule(this))
.build();
}
@Override
public void goPage(Class<? extends Activity> activity) {
Intent intent = new Intent(getActivity(), activity);
startActivity(intent);
}
@Override
public void goPage(Class<? extends Activity> activity, Bundle bundle) {
Intent intent = new Intent(getActivity(), activity);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
@Override
public void goPage(Class<? extends Activity> activity, Bundle bundle, int requestCode) {
Intent intent = new Intent(getActivity(), activity);
intent.putExtra("bundle", bundle);
startActivityForResult(intent, requestCode);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
}
@Override
public void showLoading() {
mKProgressHUD = KProgressHUD.create(getActivity());
mKProgressHUD.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setCancellable(true)
.setAnimationSpeed(2)
.setDimAmount(0.5f)
.show();
}
@Override
public void hideLoading() {
if (mKProgressHUD != null) {
mKProgressHUD.dismiss();
}
}
@Override
public void showToast(String toastStr) {
MyToast.showToast(getActivity(), toastStr);
}
}
MVP模式分工明确,的虽然使得项目结构清晰,但是他的弊端也很明显----类比较多,从而导致耦合度比较高,使用Dagger2可以进行适度的解耦。注意主要有三项,具体的就不在这里说了。
每次写一个Activity都需要手动建多个辅助类,这里可以使用我开发的一个插件,快速,完美的开发。
可以直接在插件库搜索,MVPPlug,或者你可以根据自己的项目开发适用自己项目的插件,参考地址:
https://blog.csdn.net/dong_junshuai/article/details/90672820
https://github.com/CnRiven/DaggerMvp
后续还会进行Demo的完善。