protected int mScreenHeight = 0;
protected float mScreenDensity = 0.0f;
/**
- loading view controller
*/
private VaryViewHelperController mVaryViewHelperController = null;
/**
- overridePendingTransition mode
*/
public enum TransitionMode {
LEFT, RIGHT, TOP, BOTTOM, SCALE, FADE
}
/**
- butterknife 8+ support
*/
private Unbinder mUnbinder;
/**
- databinding
*/
private View mainView;
protected ViewDataBinding binding;
/**
- default toolbar
*/
protected TextView tvTitle;
protected TextView tvRight;
protected Toolbar toolbar;
protected FrameLayout toolbarLayout;
private LinearLayout contentView;
/**
- dialog
*/
protected LoadingDialog loadingDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initCreate();
mContext = this;
// activity manager
QuickAppManager.getInstance().addActivity(this);
// animation
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);
break;
}
}
// extras
Bundle extras = getIntent().getExtras();
if (null != extras) {
getBundleExtras(extras);
}
// eventbus
EventBus.getDefault().register(this);
// screen info
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mScreenDensity = displayMetrics.density;
mScreenHeight = displayMetrics.heightPixels;
mScreenWidth = displayMetrics.widthPixels;
int layoutId = getContentViewLayoutID();
if (layoutId != 0) {
try {
// databinding support
binding = DataBindingUtil.setContentView(this, layoutId);
if (binding != null) {
mainView = binding.getRoot();
} else {
setContentView(layoutId);
}
} catch (NoClassDefFoundError e) {
setContentView(layoutId);
}
} else {
throw new IllegalArgumentException(“You must return a right contentView layout resource Id”);
}
// system status bar immersion
StatusBarUtil.setRootViewFitsSystemWindows(this, true);
StatusBarUtil.setTranslucentStatus(this);
if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
StatusBarUtil.setStatusBarColor(this, 0x55000000);
}
// network status
NetStateReceiver.registerNetworkStateReceiver(mContext);
initViewsAndEvents(savedInstanceState);
}
@Override
public void setContentView(int layoutResID) {
// 添加toolbar布局
if (isLoadDefaultTitleBar()) {
initToolBarView();
initContentView();
contentView.addView(toolbarLayout, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
View view = getContentView(layoutResID, contentView);
if (view == null) {
LayoutInflater.from(this).inflate(layoutResID, contentView, true);
super.setContentView(contentView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
} else {
super.setContentView(view, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
} else {
super.setContentView(layoutResID);
}
mUnbinder = ButterKnife.bind(this);
if (null != getLoadingTargetView()) {
mVaryViewHelperController = new VaryViewHelperController(getLoadingTargetView());
}
}
protected abstract void initCreate();
protected abstract void initDestroy();
protected abstract boolean isLoadDefaultTitleBar();
protected View getContentView(int layoutResID, LinearLayout contentView) {
return null;
}
protected void initContentView() {
contentView = new LinearLayout(this);
contentView.setOrientation(LinearLayout.VERTICAL);
}
private void initToolBarView() {
toolbarLayout = new FrameLayout(this);
LayoutInflater.from(this).inflate(R.layout.layout_toolbar, toolbarLayout,
true);
tvTitle = (TextView) toolbarLayout.findViewById(R.id.tv_title);
tvRight = (TextView) toolbarLayout.findViewById(R.id.tv_right);
toolbar = (Toolbar) toolbarLayout.findViewById(R.id.toolbar);
toolbar.setTitle(“”);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
initToolBarSet(actionBar);
}
protected void initToolBarSet(ActionBar actionBar) {
actionBar.setDisplayShowTitleEnabled(false);
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
protected Toolbar getToolbar() {
return toolbar;
}
@Override
public void finish() {
super.finish();
QuickAppManager.getInstance().removeActivity(this);
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);
break;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
initDestroy();
if (mUnbinder != null) {
mUnbinder.unbind();
}
NetStateReceiver.unRegisterNetworkStateReceiver(mContext);
EventBus.getDefault().unregister(this);
dismissLoadingDialog();
}
/**
-
get bundle data
-
@param extras
*/
protected abstract void getBundleExtras(Bundle extras);
/**
-
bind layout resource file
-
@return id of layout resource
*/
protected abstract int getContentViewLayoutID();
/**
-
when event coming
-
@param eventCenter
*/
protected abstract void onEventComing(EventCenter eventCenter);
/**
- get loading target view
*/
protected abstract View getLoadingTargetView();
/**
- init all views and add events
*/
protected abstract void initViewsAndEvents(Bundle savedInstanceState);
/**
-
toggle overridePendingTransition
-
@return
*/
protected abstract boolean toggleOverridePendingTransition();
/**
- get the overridePendingTransition mode
*/
protected abstract TransitionMode getOverridePendingTransitionMode();
/**
- apply systembar color tint
*/
protected abstract boolean isApplySystemBarTint();
/**
-
会被子类覆盖去封装 跳转到不同的activity 或者fragment的页面
-
@param clazz
-
@return
*/
protected Intent getGoIntent(Class<?> clazz) {
return new Intent(this, clazz);
}
/**
-
startActivity
-
@param clazz
*/
protected void readyGo(Class<?> clazz) {
Intent intent = getGoIntent(clazz);
startActivity(intent);
}
/**
-
startActivity with flag
-
@param clazz
-
@param flag
*/
protected void readyGo(Class<?> clazz, int flag) {
Intent intent = getGoIntent(clazz);
intent.setFlags(flag);
startActivity(intent);
}
/**
-
startActivity with bundle
-
@param clazz
-
@param bundle
*/
protected void readyGo(Class<?> clazz, Bundle bundle) {
Intent intent = getGoIntent(clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
/**
-
startActivity with bundle and flag
-
@param clazz
-
@param bundle
-
@param flag
*/
protected void readyGo(Class<?> clazz, Bundle bundle, int flag) {
Intent intent = getGoIntent(clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
intent.setFlags(flag);
startActivity(intent);
}
/**
-
startActivity then finish
-
@param clazz
*/
protected void readyGoThenKill(Class<?> clazz) {
Intent intent = getGoIntent(clazz);
startActivity(intent);
finish();
}
/**
-
startActivity with bundle then finish
-
@param clazz
-
@param bundle
*/
protected void readyGoThenKill(Class<?> clazz, Bundle bundle) {
Intent intent = getGoIntent(clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
finish();
}
/**
-
startActivityForResult
-
@param clazz
-
@param requestCode
*/
protected void readyGoForResult(Class<?> clazz, int requestCode) {
Intent intent = getGoIntent(clazz);
startActivityForResult(intent, requestCode);
}
/**
-
startActivityForResult with bundle
-
@param clazz
-
@param requestCode
-
@param bundle
*/
protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
/**
-
show toast
-
@param msg
*/
protected void showToast(String msg) {
//防止遮盖虚拟按键
if (null != msg && !StringUtil.isEmpty(msg)) {
Snackbar.make(getLoadingTargetView(), msg, Snackbar.LENGTH_SHORT).show();
}
}
/**
-
toggle show loading
-
@param toggle
*/
protected void toggleShowLoading(boolean toggle, String msg) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException(“You must return a right target view for loading”);
}
if (toggle) {
mVaryViewHelperController.showLoading(msg);
} else {
mVaryViewHelperController.restore();
}
}
/**
-
toggle show empty
-
@param toggle
*/
protected void toggleShowEmpty(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException(“You must return a right target view for loading”);
}
if (toggle) {
mVaryViewHelperController.showEmpty(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
-
toggle show error
-
@param toggle
*/
protected void toggleShowError(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException(“You must return a right target view for loading”);
}
if (toggle) {
mVaryViewHelperController.showError(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
-
toggle show network error
-
@param toggle
*/
protected void toggleNetworkError(boolean toggle, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException(“You must return a right target view for loading”);
}
if (toggle) {
mVaryViewHelperController.showNetworkError(onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
protected void toggleRestore() {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException(“You must return a right target view for loading”);
}
mVaryViewHelperController.restore();
}
@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventBus(EventCenter eventCenter) {
if (null != eventCenter) {
onEventComing(eventCenter);
}
}
/**
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
Android6.0权限控制
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
private Map<Integer, PermissionCallback> mPermissonCallbacks = null;
private Map<Integer, String[]> mPermissions = null;
public interface PermissionCallback {
/**
-
has all permission
-
@param allPerms all permissions
*/
void hasPermission(List allPerms);
/**
-
denied some permission
-
@param deniedPerms denied permission
-
@param grantedPerms granted permission
-
@param hasPermanentlyDenied has permission denied permanently
*/
void noPermission(List deniedPerms, List grantedPerms, Boolean hasPermanentlyDenied);
/**
-
@param dialogType dialogType
-
@param callback callback from easypermissions
*/
void showDialog(int dialogType, EasyPermissions.DialogCallback callback);
}
/**
-
request permission
-
@param dialogType dialogType
-
@param requestCode requestCode
-
@param perms permissions
-
@param callback callback
*/
public void performCodeWithPermission(int dialogType,
final int requestCode, @NonNull String[] perms, @NonNull PermissionCallback callback) {
if (EasyPermissions.hasPermissions(this, perms)) {
callback.hasPermission(Arrays.asList(perms));
} else {
if (mPermissonCallbacks == null) {
mPermissonCallbacks = new HashMap<>();
}
mPermissonCallbacks.put(requestCode, callback);
if (mPermissions == null) {
mPermissions = new HashMap<>();
总结
其实要轻松掌握很简单,要点就两个:
- 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
- 多练。 (视频优势是互动感强,容易集中注意力)
你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
Callback callback);
}
/**
-
request permission
-
@param dialogType dialogType
-
@param requestCode requestCode
-
@param perms permissions
-
@param callback callback
*/
public void performCodeWithPermission(int dialogType,
final int requestCode, @NonNull String[] perms, @NonNull PermissionCallback callback) {
if (EasyPermissions.hasPermissions(this, perms)) {
callback.hasPermission(Arrays.asList(perms));
} else {
if (mPermissonCallbacks == null) {
mPermissonCallbacks = new HashMap<>();
}
mPermissonCallbacks.put(requestCode, callback);
if (mPermissions == null) {
mPermissions = new HashMap<>();
总结
其实要轻松掌握很简单,要点就两个:
- 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
- 多练。 (视频优势是互动感强,容易集中注意力)
你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。
[外链图片转存中…(img-r0LRIjP6-1715339937978)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!