基于Mvp和BaseActivity简单的封装,小白可以直接拿来上手用.

1.主要是这5个类,算是5个基类的封装.

基于Mvp和BaseActivity简单的封装,小白可以直接拿来上手用._第1张图片

2.具体直接上代码

(第一个)BasePresenter

public interface  BasePresenter {
    void attachView(V view);

    void detachView();
}

(第二个)BasePresenterImpl

public class BasePresenterImpl implements BasePresenter {
    protected V mView;
    @Override
    public void attachView(V view) {
        mView=view;
    }

    @Override
    public void detachView() {
        mView=null;
    }
}

第三个(BaseView)

public interface BaseView {
     Context getContext();
}

第四个(MVPBaseActivity)

public abstract class MVPBaseActivity> extends
        FragmentActivity implements BaseView {

    public T mPresenter;

 

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
    
        mPresenter = getInstance(this, 1);
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        if (getContentViewId() != 0) {
            setContentView(getContentViewId());
        }
//        StatusBarUtil.setColor(this,getResources()
//                .getColor(R.color.title),0);
//        StatusBarUtil.setDarkMode(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(mActivity);
        initProgressDialog();
        initViews();//初始化布局文件
        initEvents();//事件
    }


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

    @Override
    public Context getContext() {
        return this;
    }

    public  T getInstance(Object o, int i) {
        try {
            return ((Class) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取布局id
     *
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 初始化布局
     */
    protected abstract void initViews();

    /**
     * 事件
     */
    protected abstract void initEvents();


    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(BaseApplication.getAppComponent())
                .activityModule(new ActivityModule())
                .build();
    }
}

第5个(MvpBaseFragment)

public abstract class MVPBaseFragment> extends Fragment implements BaseView {
    public T mPresenter;
    protected Activity mActivity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(getViewId(), container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        initProgressDialog();
        initViews();
        initEvents();
    }


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

    @Override
    public Context getContext() {
        return super.getContext();
    }

    public  T getInstance(Object o, int i) {
        try {
            return ((Class) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 获取view id
     */
    protected abstract int getViewId();

    /**
     * 初始化布局
     */
    protected abstract void initViews();

    /**
     * 事件
     */
    protected abstract void initEvents();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}

3.具体事例应用

MainContract代码

public class MainContract {
    interface View extends BaseView {
        void showData();
    }

    interface Prestenter extends BasePresenter {
        void loadData();
    }
}

MainPresenter代码

public class MainPresenter extends BasePresenterImpl
        implements MainContract.Prestenter {

    @Override
    public void loadData() {
        mView.showData();
    }
}

MainActivity代码

public class MainActivity extends MVPBaseActivity
        implements MainContract.View {

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        mPresenter.loadData();
    }

    @Override
    public void showData() {
        toast("哈哈哈哈哈哈哈哈啊哈!");
    }
}

下载地址:https://download.csdn.net/download/qq1021380645/10813100

你可能感兴趣的:(mvp封装,mvp)