1. 程式人生 > >Android 自定義Mvp架構

Android 自定義Mvp架構

/**
 * Author:DoctorWei
 * Time:2018/12/4 20:34
 * Description:
 * email:[email protected]
 */
1.定義BaseView回撥介面
public interface BaseView {
}

2.定義IBasePresenter業務回撥
public interface IBasePresenter <V extends BaseView>{ }

3.定義BasePresenter業務回撥

public class BasePresenter<V extends BaseView> implements IBasePresenter<V> {
    private WeakReference<V> weakView;
    private V baseView;
    public void attachView(V baseView){
        this.weakView=new WeakReference<V>(baseView);
    }
    public void dettachView(){
        if (this.weakView!=null){
            this.weakView.clear();
            this.weakView=null;
        }
    }
    public V getView(){
        return baseView;
    }
    /**
     * 用於檢查View是否為空物件
     */
    public boolean isAttachView(){
        return this.weakView!=null&&this.weakView.get()!=null;
    }
}
4.定義BaseActivity將Presenter和View進行繫結
public abstract class BaseActivity<V extends BaseView,P extends BasePresenter> extends SupportActivity implements BaseView{
    public static final String TAG=BaseActivity.class.getSimpleName();
    public P mPresenter;
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d(TAG,"onCreate");
        setContentView(getLayoutId());
        initView();
        initPresenter();
        initData(savedInstanceState);
    }

    protected void initPresenter() {
        mPresenter=createPresenter();
        if (mPresenter!=null){
            mPresenter.attachView(this);
        }
    }
    protected void hasViewAndPresenter(ExcutePresenter<P> excutePresenter){
        boolean hasViewAndPresenter = mPresenter != null && mPresenter.isAttachView();
        if (hasViewAndPresenter){
            excutePresenter.excute(mPresenter);
        }else{
            Logger.d("mvp未繫結");
        }
    }
    public interface ExcutePresenter<P>{
        void excute(P presenter);
    }
    protected P getPresenter() {
        return mPresenter;
    }
    protected V getView() {
        return (V)this;
    }

    protected abstract P createPresenter();


    protected abstract void initData(Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract int getLayoutId();

    //寫一個全域性的跳轉
    public  void openActivityWithIntent(@NonNull Class activity,@NonNull Bundle bundle){
        Intent intent=new Intent(this,activity);
        if (bundle!=null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
    public Bundle getIntentBundle(){
        Bundle bundle = getIntent().getExtras();
        return bundle;
    }
    /**
     * 列印Activity的生命週期
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        Logger.d(TAG,"onRestart");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Logger.d(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Logger.d(TAG,"onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.d(TAG,"onDestroy");
        if (mPresenter!=null){
            mPresenter.dettachView();
        }
    }
}