昨天我們簡單介紹了MVP 并封裝了View 和Presenter 今天我們來看在BaseActivity
主要是抽取一些公共的方法? 比如綁定view 設(shè)置標(biāo)題? 還有界面跳轉(zhuǎn) 還有導(dǎo)航欄的顏色
下面是所有代碼
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.dasheng.mvp.mvpdemo.R;
import com.dasheng.mvp.mvpdemo.view.Eyes;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* 所有Activity的基類
* 作者 :created by 大圣 on 2018/11/6 11 : 09
* 郵箱 :601864175@qq.com
*/
public abstract class BaseActivity? extends AppCompatActivityimplements BaseView {
private UnbindermUbinder ;
? ? private T mPresenter;
? ? protected enum TransitionMode {
LEFT, RIGHT, TOP, BOTTOM, SCALL, FADE
? ? }
@Override
? ? protected void onCreate(@Nullable Bundle savedInstanceState) {
if (toggleOverridePendingTransition()){
//? ? ? ? ? ? switch (getOverridePendingTransitionMode()) {
//? ? ? ? ? ? ? ? case LEFT:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.left_in,R.anim.left_out);
//
//? ? ? ? ? ? ? ? ? ? break;
//? ? ? ? ? ? ? ? case RIGHT:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.right_in, R.anim.right_out);
//? ? ? ? ? ? ? ? ? ? break;
//? ? ? ? ? ? ? ? case TOP:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.top_in, R.anim.top_out);
//? ? ? ? ? ? ? ? ? ? break;
//? ? ? ? ? ? ? ? case BOTTOM:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
//? ? ? ? ? ? ? ? ? ? break;
//? ? ? ? ? ? ? ? case SCALE:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.scale_in, R.anim.scale_out);
//? ? ? ? ? ? ? ? ? ? break;
//? ? ? ? ? ? ? ? case FADE:
//? ? ? ? ? ? ? ? ? ? overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//
//? ? ? ? ? ? }
? ? ? ? }
super.onCreate(savedInstanceState);
? ? ? ? setContentView(getLayout());
? ? ? ? mUbinder = ButterKnife.bind(this);
? ? ? ? createPresenter();
? ? ? ? if (null !=mPresenter)
mPresenter.attachView(this);
? ? ? ? APP.getInstance().setAllActivity(this);
? ? ? ? initEventAndData();
? ? }
@Override
? ? protected void onDestroy() {
super.onDestroy();
? ? ? ? if (null !=mPresenter)
mPresenter.detachView();
? ? ? ? mUbinder.unbind();
? ? ? ? APP.getInstance().removeActivity(this);
? ? }
@Override
? ? public void showErrorail(String msg) {
}
public void launchActivity(Class c) {
Intent intent =new Intent(this, c);
? ? ? ? startActivity(intent);
? ? }
public void launchActivity(Class c, Bundle bundle) {
if (bundle !=null) {
Intent intent =new Intent(this, c);
? ? ? ? ? ? intent.putExtras(bundle);
? ? ? ? ? ? startActivity(intent);
? ? ? ? }
}
@Override
? ? public void isShow(boolean isShow) {
}
@LayoutRes
? ? protected abstract int getLayout();
? ? //每個(gè)activity都需要創(chuàng)建Presenter
? ? protected abstract void initEventAndData();
? ? protected abstract boolean toggleOverridePendingTransition();
? ? /**
? ? * 切換加載動畫
? ? * @author 大圣
? ? * @time 2018/11/16 下午4:55
*/
? ? protected abstract TransitionModegetOverridePendingTransitionMode();
? ? protected abstract void? createPresenter();
? ? protected abstract boolean setStatusBarColor();
? ? protected void setStaTitleColor(boolean i,int id){
if (i ){
//狀態(tài)欄設(shè)置為透明狀態(tài)欄
? ? ? ? ? ? Eyes.translucentStatusBar(this,true);
? ? ? ? }else{
//狀態(tài)欄設(shè)置為指定顏色
? ? ? ? ? ? Eyes.setStatusBarColor(this, getResources().getColor(id));
? ? ? ? }
}
}