1.设计模式的定义
2.模板设计模式
定义一个操作中的算法的框架,而将一些步骤延迟到子类中。使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤
UML类图(参考android源码设计模式)
package com.example.michael.rdpad.base.activity;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.apkfuns.logutils.LogUtils;
import com.example.michael.rdpad.R;
import com.example.michael.rdpad.base.fragment.BaseFragment;
import com.example.michael.rdpad.contants.ChangeAnimType;
import com.example.michael.rdpad.utils.CrashUtils;
import com.example.michael.rdpad.utils.NetWorkUtils;
import com.example.michael.rdpad.widget.LoadDataView;
import com.example.michael.rdpad.widget.LoadingProgress;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
//import com.githang.statusbar.StatusBarCompat;
/**
* Created by Michael on 2016/04/19
*/
public abstract class BaseActivity
extends AppCompatActivity {
public static final int LOAD_MORE = 1;
public static final int REFRESH = 2;
public static final int INIT = 0;
private Window window;
protected LoadingProgress loadingProgress;
private boolean destroyed = false;
private static Handler handler;
private boolean isVisible;
private DialogInterface.OnDismissListener listener;
protected FragmentManager mFragmentManager;
protected int mLoadDataType = 0;
protected View activityView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
activityView = LayoutInflater.from(this).inflate(getLayoutId(), null);
setContentView(activityView);
mFragmentManager = getSupportFragmentManager();
initWindow();
//init();
isVisible = true;
initLoadingProgress();
init();
//StatusBarUtil.StatusBarLightMode(this);
}
/*private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
private ProgressBar pb;
private TextView tvDownload;
public static void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}*/
protected void init() {
parseIntent();
initView();
initData();
updateView();
initListener();
initAdapter();
}
protected abstract void initAdapter();
protected abstract void initListener();
protected abstract void updateView();
protected abstract void initData();
protected abstract void initView();
@Override
public void recreate() {
if (mFragmentManager != null && mFragmentManager.getFragments() != null) {
int size = mFragmentManager.getFragments().size();
for (int i = 0; i < size; i++) {
mFragmentManager.popBackStack();
}
}
super.recreate();
}
private void initLoadingProgress() {
loadingProgress = new LoadingProgress(this);
loadingProgress.setCancelable(false);
loadingProgress.setCanceledOnTouchOutside(false);
listener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
progressDismissListener();
}
};
}
protected void refreshViewStatus(XRecyclerView xRecyclerView) {
if (mLoadDataType == LOAD_MORE) {
xRecyclerView.loadMoreComplete();
} else if (mLoadDataType == REFRESH) {
xRecyclerView.refreshComplete();
}
}
public abstract int getLayoutId();
@Override
protected void onDestroy() {
super.onDestroy();
}
public void removeFragment() {
if (loadingProgress != null && loadingProgress.isShowing()) {
loadingProgress.dismiss();
}
if (mFragmentManager == null) {
mFragmentManager = getSupportFragmentManager();
}
LogUtils.d("回退fragment count " + mFragmentManager.getBackStackEntryCount());
if (mFragmentManager.getBackStackEntryCount() > 1) {
mFragmentManager.popBackStack();
} else {
finish();
}
}
protected void progressDismissListener() {
}
public void progressDismiss() {
try {
if (isVisible && loadingProgress != null) {
if (loadingProgress.isShowing()) {
loadingProgress.dismiss();
}
}
} catch (Exception e) {
CrashUtils.crash(e, "进度加载异常!");
}
}
public void progressShow() {
if (isVisible) {
if (loadingProgress != null) {
if (!loadingProgress.isShowing()) {
loadingProgress.show();
loadingProgress.setOnDismissListener(null);
}
}
}
}
public void loadingProgressShowTimeOut() {
if (loadingProgress != null) {
if (!loadingProgress.isShowing()) {
loadingProgress.show(true);
loadingProgress.setOnDismissListener(listener);
}
}
}
protected final Handler getHandler() {
if (handler == null) {
handler = new Handler(getMainLooper());
}
return handler;
}
@Override
public void onBackPressed() {
removeFragment();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
removeFragment();
}
return true;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
protected void showKeyboard(boolean isShow) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (isShow) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
if (getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} else {
LogUtils.d("get current focus is null");
hideInputMethod(getWindow().getDecorView());
}
}
}
protected void hideInputMethod(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
protected void showKeyboardDelayed(View focus) {
final View viewToFocus = focus;
if (focus != null) {
focus.requestFocus();
}
getHandler().postDelayed(new Runnable() {
@Override
public void run() {
if (viewToFocus == null || viewToFocus.isFocused()) {
showKeyboard(true);
}
}
}, 200);
}
public boolean isDestroyedCompatible() {
if (Build.VERSION.SDK_INT >= 17) {
return isDestroyedCompatible17();
} else {
return destroyed || super.isFinishing();
}
}
@TargetApi(17)
private boolean isDestroyedCompatible17() {
return super.isDestroyed();
}
public void addFragment(int layoutId, BaseFragment baseFragment, int animType) {
if (mFragmentManager == null) {
mFragmentManager = getSupportFragmentManager();
}
FragmentTransaction transaction = mFragmentManager.beginTransaction();
setAnimation(animType, transaction);
transaction.add(layoutId, baseFragment)
.addToBackStack(baseFragment.getClass().getSimpleName())
.commitAllowingStateLoss();
}
private void setAnimation(int animType, FragmentTransaction transaction) {
if (animType == ChangeAnimType.LEFT_RIGHT) {
transaction.setCustomAnimations(R.anim.in_from_right, R.anim.out_from_left, R.anim.in_from_left,
R.anim.out_from_right);
} else if (animType == ChangeAnimType.SCALE) {
transaction.setCustomAnimations(R.anim.scale_to_one, R.anim.scale_to_zero);
} else if (animType == ChangeAnimType.TOP_TO_LOW) {
transaction.setCustomAnimations(R.anim.slide_in_from_top, R.anim.slide_out_to_top, R.anim.slide_in, R.anim.slide_out_to_top);
} else if (animType == ChangeAnimType.LOW_TO_TOP_) {
transaction.setCustomAnimations(R.anim.slide_in_from_bottom, R.anim.slide_out_to_bottom, R.anim.slide_in, R.anim.slide_out_to_bottom);
}
}
public BaseFragment replaceFragment(int layoutId, BaseFragment fragment, int animType) {
return replaceFragment(layoutId, fragment, animType, true);
}
public BaseFragment replaceFragment(int layoutId, BaseFragment fragment, int animType, boolean isInPop) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
setAnimation(animType, transaction);
transaction.replace(layoutId, fragment);
if (isInPop) {
transaction.addToBackStack(fragment.getClass().getSimpleName());
}
try {
transaction.commitAllowingStateLoss();
} catch (Exception e) {
}
return fragment;
}
private void initWindow() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
initWindowBarColor(getStatusColor(0));
//window.setNavigationBarColor(Color.TRANSPARENT);
ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, true);
}
}
}
protected int getStatusColor(int color) {
if (color == 0) {
return getResources().getColor(R.color.colorAccent);
}
return color;
}
protected boolean isCompatible(int apiLevel) {
return Build.VERSION.SDK_INT >= apiLevel;
}
protected T findId(int resId) {
return (T) (findViewById(resId));
}
protected T findId(View view, int resId) {
return (T) (view.findViewById(resId));
}
protected void initWindowBarColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(color);
}
}
protected void parseIntent() {
if (getIntent() != null) {
LogUtils.d(getIntent().getExtras());
}
}
public void tgtInvalid(String message) {
}
}