昨天我们简单介绍了MVP 并封装了View 和Presenter 今天我们来看在BaseActivity
主要是抽取一些公共的方法 比如绑定view 设置标题 还有界面跳转 还有导航栏的颜色
下面是所有代码
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();
//每个activity都需要创建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 ){
//状态栏设置为透明状态栏
Eyes.translucentStatusBar(this,true);
}else{
//状态栏设置为指定颜色
Eyes.setStatusBarColor(this, getResources().getColor(id));
}
}
}