BaseActivity和BaseFragment 的封装

来源:互联网 发布:虚拟机优化显卡性能 编辑:程序博客网 时间:2024/05/22 02:13
  1. 修改BaseActiviy的继承为Fragment(可以实现dialogfragment)
  2. 增加isDebug和APP_NAME,在Application中声明,方便日志打印,区分测试版本和正式版本
  3. 添加快速点击导致的各种问题。

public abstract class BaseActivity extends FragmentActivity implementsOnClickListener {/** 是否沉浸状态栏 **/private boolean isSetStatusBar = true;/** 是否允许全屏 **/private boolean mAllowFullScreen = true;/** 是否禁止旋转屏幕 **/private boolean isAllowScreenRoate = false;/** 当前Activity渲染的视图View **/private View mContextView = null;/** 是否输出日志信息 **/private boolean isDebug;private String APP_NAME;protected final String TAG = this.getClass().getSimpleName();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);isDebug = MApplication.isDebug;APP_NAME = MApplication.APP_NAME;$Log(TAG + "-->onCreate()");try {Bundle bundle = getIntent().getExtras();initParms(bundle);mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);if (mAllowFullScreen) {this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE);}if (isSetStatusBar) {steepStatusBar();}setContentView(mContextView);if (!isAllowScreenRoate) {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);} else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}initView(mContextView);doBusiness(this);} catch (Exception e) {e.printStackTrace();}}/** * [沉浸状态栏] */private void steepStatusBar() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 透明状态栏getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);// 透明导航栏getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}}/** * [初始化Bundle参数] *  * @param parms */public abstract void initParms(Bundle parms);/** * [绑定布局] *  * @return */public abstract int bindLayout();/** * [重写: 1.是否沉浸状态栏 2.是否全屏 3.是否禁止旋转屏幕] */// public abstract void setActivityPre();/** * [初始化控件] *  * @param view */public abstract void initView(final View view);/** * [业务操作] *  * @param mContext */public abstract void doBusiness(Context mContext);/** View点击 **/public abstract void widgetClick(View v);@Overridepublic void onClick(View v) {if (fastClick())widgetClick(v);}/** * [页面跳转] *  * @param clz */public void startActivity(Class<?> clz) {startActivity(clz, null);}/** * [携带数据的页面跳转] *  * @param clz * @param bundle */public void startActivity(Class<?> clz, Bundle bundle) {Intent intent = new Intent();intent.setClass(this, clz);if (bundle != null) {intent.putExtras(bundle);}startActivity(intent);}@SuppressWarnings("unchecked")public <T extends View> T $(int resId) {return (T) super.findViewById(resId);}/** * [含有Bundle通过Class打开编辑界面] *  * @param cls * @param bundle * @param requestCode */public void startActivityForResult(Class<?> cls, Bundle bundle,int requestCode) {Intent intent = new Intent();intent.setClass(this, cls);if (bundle != null) {intent.putExtras(bundle);}startActivityForResult(intent, requestCode);}@Overrideprotected void onResume() {super.onResume();$Log(TAG + "--->onResume()");}@Overrideprotected void onDestroy() {super.onDestroy();$Log(TAG + "--->onDestroy()");}/** * [是否允许全屏] *  * @param allowFullScreen */public void setAllowFullScreen(boolean allowFullScreen) {this.mAllowFullScreen = allowFullScreen;}/** * [是否设置沉浸状态栏] *  * @param allowFullScreen */public void setSteepStatusBar(boolean isSetStatusBar) {this.isSetStatusBar = isSetStatusBar;}/** * [是否允许屏幕旋转] *  * @param isAllowScreenRoate */public void setScreenRoate(boolean isAllowScreenRoate) {this.isAllowScreenRoate = isAllowScreenRoate;}/** * [日志输出] *  * @param msg */protected void $Log(String msg) {if (isDebug) {Log.d(APP_NAME, msg);}}/** * [防止快速点击] *  * @return */private boolean fastClick() {long lastClick = 0;if (System.currentTimeMillis() - lastClick <= 1000) {return false;}lastClick = System.currentTimeMillis();return true;}}
public abstract class BaseFragment extends Fragment implements OnClickListener {private boolean isDebug;private String APP_NAME;protected final String TAG = this.getClass().getSimpleName();private View mContextView = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);isDebug = MApplication.isDebug;APP_NAME = MApplication.APP_NAME;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {mContextView = inflater.inflate(bindLayout(), container, false);initView(mContextView);doBusiness(getActivity());return mContextView;}/** * [绑定布局] *  * @return */public abstract int bindLayout();/** * [初始化控件] *  * @param view */public abstract void initView(final View view);/** * [业务操作] *  * @param mContext */public abstract void doBusiness(Context mContext);/** View点击 **/public abstract void widgetClick(View v);@Overridepublic void onClick(View v) {if (fastClick())widgetClick(v);}@SuppressWarnings("unchecked")public <T extends View> T $(View view, int resId) {return (T) view.findViewById(resId);}/** * [日志输出] *  * @param msg */protected void $Log(String msg) {if (isDebug) {Log.d(APP_NAME, msg);}}/** * [防止快速点击] *  * @return */private boolean fastClick() {long lastClick = 0;if (System.currentTimeMillis() - lastClick <= 1000) {return false;}lastClick = System.currentTimeMillis();return true;}
0 1