Android之Activity基類封裝

lvxiangan發表於2019-01-15

檢視相關
一般的Activity裡都會用到很多的findViewById這個方法,而且每次都要強制型別轉換,這樣會顯得很繁瑣,如果在BaseActivity裡封裝好,就能省事:

protected <T extends View> T findView(int id) {
    return (T) findViewById(id);
}

這樣只要是繼承了BaseActivity就能輕鬆使用LinearLayout llContent = findView(R.id.ll_content);,免去了諸多型別轉換的麻煩。

 

參考程式碼:

public abstract class BaseActivity extends FragmentActivity implements
		OnClickListener {
	/** 是否沉浸狀態列 **/
	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();

	@Override
	protected 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);

	@Override
	public 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);
	}

	@Override
	protected void onResume() {
		super.onResume();
		$Log(TAG + "--->onResume()");
	}

	@Override
	protected 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;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		isDebug = MApplication.isDebug;
		APP_NAME = MApplication.APP_NAME;
	}

	@Override
	public 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);

	@Override
	public 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 long lastClick = 0;
	private boolean fastClick() {
		if (System.currentTimeMillis() - lastClick <= 1000) {
			return false;
		}
		lastClick = System.currentTimeMillis();
		return true;
	}
}

 

相關文章