一.都说好的封装能省不少事情,也能很好的解决代码的耦合性,减少后期维护项目的成本,好了废话不多说了我们来看看代码
1.这是封装好的BaseActivity
public abstract class BaseActivity<T extends IPresenter> extends AppCompatActivity {
protected T mPresenter;
private LoadingDialog loadingDialog;
protected AppCompatActivity mActivity;
@Override
public void startActivity(Intent intent) {
super.startActivity(intent);
boolean animition = intent.getBooleanExtra("animition", true);
if (animition) {
overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_right_reman);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
setContentView(getLayoutId());
ButterKnife.bind(this);
mActivity = this;
AppManager.getAppManager().addActivity(this);
mPresenter = getPresenter();
if (getToolBar() != null) {
getToolBar().setTitle("");
setSupportActionBar(getToolBar());
}
initData();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
onClick();
if (!this.getClass().getSimpleName().equals("MainActivity") && !this.getClass().getSimpleName().equals("SearchActivity")
&&!this.getClass().getSimpleName().equals("InviteActivity")
&&!this.getClass().getSimpleName().equals("WillActivity")) {
setDrawable(0);
}
if (getToolBar() != null) {
getToolBar().setNavigationOnClickListener(view -> finish());
}
}
@Override
public void finish() {
super.finish();
boolean animition = getIntent().getBooleanExtra("animition", true);
if (animition) {
overridePendingTransition(0, R.anim.base_slide_right_out);
}
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
boolean animition = intent.getBooleanExtra("animition", true);
if (animition) {
overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_right_reman);
}
}
@Override
protected void onStart() {
if (getToolBar() != null){
getToolBar().setNavigationIcon(R.mipmap.home_page_details_return);
}
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.unSubcrible();
}
dismissLoadingDialog();
AppManager.getAppManager().finishActivity(this);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, R.anim.base_slide_right_out);
}
protected abstract T getPresenter();
protected abstract int getLayoutId();
protected abstract void initData();
protected abstract void onClick();
protected abstract Toolbar getToolBar();
protected void setDrawable(int drawable) {
if (drawable != 0) {
getToolBar().setNavigationIcon(getResources().getDrawable(drawable));
} else {
getToolBar().setNavigationIcon(getResources().getDrawable(R.mipmap.ic_back));
}
}
protected void logD(String log) {
LogUtils.d(this, log);
}
protected void logE(String log) {
LogUtils.e(this, log);
}
protected void toaskMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
protected void showLoadingDialog() {
if (loadingDialog == null) {
loadingDialog = new LoadingDialog(this);
}
loadingDialog.show();
}
protected void dismissLoadingDialog() {
if (loadingDialog != null && loadingDialog.isShow()) {
loadingDialog.close();
}
}
}
上面封装的东西很多了,各位可以视情况减少或添加
2.下面是fragment的封装
public abstract class BaseFragment<T extends IPresenter> extends Fragment {
protected T mPresenter;
protected View view;
private LoadingDialog loadingDialog;
protected Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(getLayoutId(), container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
context = getActivity();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mPresenter = getPresenter();
initData();
onClick();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
ButterKnife.unbind(this);
if (mPresenter != null)
mPresenter.unSubcrible();
}
protected abstract T getPresenter();
protected abstract int getLayoutId();
protected abstract void initData();
protected abstract void onClick();
protected void logD(String log) {
LogUtils.d(context, log);
}
protected void logE(String log) {
LogUtils.e(context, log);
}
protected void toaskMsg(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
protected void showLoadingDialog() {
if (loadingDialog == null) {
loadingDialog = new LoadingDialog(context);
}
loadingDialog.show();
}
protected void dismissLoadingDialog() {
if (loadingDialog != null && loadingDialog.isShow()) {
loadingDialog.close();
}
}
protected boolean getLogin(){
return MvpContactApplication.getInstance().appData.getLogin();
}
}
3.下面是presenter的封装
public class BasePresenter<T extends IBaseView> implements IPresenter {
private T mView;
protected CompositeDisposable compositeSubscription;
private LoadingDialog loadingDialog;
public BasePresenter(T view) {
this.mView = view;
}
protected void unSubscribe() {
if (compositeSubscription != null) {
compositeSubscription.dispose();
}
}
public void showDialog() {
loadingDialog = new LoadingDialog((Activity) mView.getMyContext());
loadingDialog.show();
}
public void closeDialog() {
if (loadingDialog != null) {
loadingDialog.close();
}
}
protected void addSubscribe(Disposable subscription) {
if (compositeSubscription == null) {
compositeSubscription = new CompositeDisposable();
}
compositeSubscription.add(subscription);
}
protected Context getContext() {
return mView.getMyContext();
}
@Override
public void unSubcrible() {
this.mView = null;
unSubscribe();
}
}
4.IBaseView里面一个接口
public interface IBaseView {
Context getMyContext();
void error();
}
5.IPresenter的接口
public interface IPresenter {
void unSubcrible();
}
基类的封装因人而异,也仅仅给大家提供一个思路,具体设计得看需求的分析,欢迎各位技术大牛吐槽切磋,我虚心接受。
技术交流qq:1129126470
WeChat:ym1129126470,不吝赐教 转载请注明出处,尊重知识。