封装MVP模式
程序员文章站
2022-06-08 22:19:10
...
1.创建View、Presenter和Model的接口
public interface IView {
void showLoadDialog();
void dismissLoadDialog();
void showLongToast(String msg);
void showShortToast(String msg);
Boolean isNetworkConnected();
void startActivity(Class<?> clz);
void startActivity(Class<?> clz, @NonNull Bundle bundle);
void startActivityForResult(Class<?> clz, int requestCode);
void startActivityForResult(Class<?> clz, int requestCode, @NonNull Bundle bundle);
Bundle getIntentBundle();
}
public interface IPresenter<V extends IView> {
//绑定view类
void bindingView(V view);
void onDestroy();
}
public interface IModel {
void onDestroy();
}
2.创建Presenter和Model的基类
public abstract class BasePresenter<M extends IModel, V extends IView> implements IPresenter<V> {
public V mView;
public M mModel;
public abstract M bindingModel();
@Override
public void bindingView(V view) {
mView = view;
mModel = bindingModel();
}
@Override
public void onDestroy() {
mModel.onDestroy();
mModel = null;
mView = null;
}
}
public abstract class BaseModel implements IModel{
@Override
public void onDestroy() {
}
}
3.BaseMvpActivity基类
BaseActivity代码地址:https://blog.csdn.net/a896159476/article/details/102778772
public abstract class BaseMvpActivity<V extends IView,P extends IPresenter<V>> extends BaseActivity implements IView {
public P presenter;
protected abstract P createPresenter();
@Override
public void initView() {
presenter = createPresenter();
presenter.bindingView((V)this);
}
@Override
public void startActivity(Class<?> clz) {
super.startActivity(clz);
}
@Override
public void startActivity(Class<?> clz, @NonNull Bundle bundle) {
super.startActivity(clz, bundle);
}
@Override
public void startActivityForResult(Class<?> clz, int requestCode) {
super.startActivityForResult(clz, requestCode);
}
@Override
public void startActivityForResult(Class<?> clz, int requestCode, @NonNull Bundle bundle) {
super.startActivityForResult(clz, requestCode, bundle);
}
@Override
public Bundle getIntentBundle() {
return super.getIntentBundle();
}
/**
* 如果未注册[registerNetworkMonitoring] 会抛出空指针异常
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Boolean isNetworkConnected() {
return super.isNetworkConnected();
}
@Override
public void showLoadDialog() {
super.showLoadDialog();
}
@Override
public void dismissLoadDialog() {
super.dismissLoadDialog();
}
@Override
public void showLongToast(String msg) {
super.showLongToast(msg);
}
@Override
public void showShortToast(String msg) {
super.showShortToast(msg);
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
presenter = null;
}
}
4.创建契约类
public interface MainContract{
interface View extends IView{
}
interface Presenter extends IPresenter<View> {
}
interface Model extends IModel {
}
}
5.Activity继承BaseMvpActivity实现View接口
public class MainActivity extends BaseMvpActivity<MainContract.View,MainContract.Presenter> implements MainContract.View{
@Override
protected int attachLayoutRes() {
return R.layout.activity_main;
}
@Override
protected MainContract.Presenter createPresenter() {
return new MainPresenter();
}
@Override
public void initView() {
}
@Override
public void initData() {
}
}
6.创建Presenter
public class MainPresenter extends BasePresenter<MainContract.Model, MainContract.View> implements MainContract.Presenter {
@Override
public MainContract.Model bindingModel() {
return new MainModel();
}
}
7.创建Model
public class MainModel extends BaseModel implements MainContract.Model{
}
8.MVPFragment
BaseFragment:https://blog.csdn.net/a896159476/article/details/102781914
public abstract class BaseMvpFragment<V extends IView, P extends IPresenter<V>> extends BaseFragment implements IView {
public P presenter;
protected abstract P createPresenter();
@Override
public void initView(View view) {
presenter = createPresenter();
presenter.bindingView((V) this);
}
@Override
public void startActivity(Class<?> clz) {
super.startActivity(clz);
}
@Override
public void startActivity(Class<?> clz, @NonNull Bundle bundle) {
super.startActivity(clz, bundle);
}
@Override
public void startActivityForResult(Class<?> clz, int requestCode) {
super.startActivityForResult(clz, requestCode);
}
@Override
public void startActivityForResult(Class<?> clz, int requestCode, @NonNull Bundle bundle) {
super.startActivityForResult(clz, requestCode, bundle);
}
@Override
public Bundle getIntentBundle() {
return super.getIntentBundle();
}
/**
* 如果未注册[registerNetworkMonitoring] 会抛出空指针异常
*/
@Override
public Boolean isNetworkConnected() {
return super.isNetworkConnected();
}
@Override
public void showLoadDialog() {
super.showLoadDialog();
}
@Override
public void dismissLoadDialog() {
super.dismissLoadDialog();
}
@Override
public void showLongToast(String msg) {
super.showLongToast(msg);
}
@Override
public void showShortToast(String msg) {
super.showShortToast(msg);
}
}
上一篇: winform 系统对话框、右键菜单、状态栏(补充)
下一篇: RichTextBox文件格式错误