MVP 库

package com.example.mvp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.mvp.mvp.BaseModel;
import com.example.mvp.mvp.BasePresenter;
import com.example.mvp.mvp.IBaseView;

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView {
    public P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayoutId());
        initView();
        presenter=initPresenter();
        if(presenter!=null){
            presenter.attch(initModel(),this);
        }
    }

    /**
     * 初始化子类Model
     * @return
     */
    protected abstract BaseModel initModel();

    /**
     * 让子类初始化p
     * @return
     */
    protected abstract P initPresenter();

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 绑定资源
     * @return
     */
    protected abstract int bindLayoutId();

    /**
     * 连接失败
     * @param msg
     */
    public void serverFail(String msg){

    }
    public void showLoading(){

    }
    public void hideLoading(){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(presenter!=null){
            presenter.detach();
        }
    }
}

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IBaseView{
    public P presenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(bindLayoutId(),container,false);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter=initPresenter();
        if(presenter!=null){
            Log.e("BaseFragment","presenter");
            presenter.attch(initModel(),this);
        }
        initData();
    }

    protected abstract void initData();


    /**
     * 初始化子类model
     * @return
     */
    protected abstract BaseModel initModel();

    /**
     * 让子类出示化P
     * @return
     */
    protected abstract P initPresenter();

    /**
     * 初始化控件
     * @param view
     */
    protected abstract void initView(View view);

    /**
     * 绑定资源
     * @return
     */
    protected abstract int bindLayoutId();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(presenter!=null){
            presenter.detach();
        }
    }

}

public class BaseModel implements IBaseModel{

    @Override
    public void serverFail(String msg) {

    }
}

public class BasePresenter<M extends BaseModel,V extends IBaseView> implements IBasePresenter {
    public M model;
    public V view;

    /**
     * 绑定view和model
     * @param m
     * @param v
     */
    public void attch(M m,V v){
        this.model=m;
        this.view=v;
    }

    /**
     * 回收资源,解绑,避免内存泄漏
     */
    public void detach(){
        this.model=null;
        this.view=null;
    }
}

public interface IBaseModel {
    void serverFail(String msg);
}
public interface IBasePresenter {
}
public interface IBaseView {
    void showLoading();
    void hideLoading();
    void serverFail(String msg);
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页