封装的基础类----BaseActivity
需求要点:
- 显示进度的dialog;
- 自定义Actionbar;
- 返回按钮的处理;
- 是否显示状态栏;
- activity的管理;
- 自适应布局控件;
实现:
BaseActivity.java
public abstract class BaseActivity extends AppActivity implements DialogControl,
EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {
private static final String TAG = "BaseActivity";
protected static final int REQUEST_CODE_SETTINGS = 101;
protected static final int RC_PHONE_AND_STORAGE = 201;
protected static final String[] PERMS_PHONE_AND_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE};
private Dialog mWaitDialog;
protected BaseAppBarCompat mAppBarCompat = new BaseAppBarCompat();
@Nullable
@Override
public AppBarConfig getAppBarConfig() {
return null;
}
@Override
public void onBackPressed() {
hideWaitDialog();
super.onBackPressed();
}
@Override
public void hideWaitDialog() {
try {
if (mWaitDialog != null) {
mWaitDialog.dismiss();
}
mWaitDialog = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public Dialog showWaitDialog() {
return showWaitDialog(R.string.wait_loading);
}
@Override
public Dialog showWaitDialog(int resid) {
return showWaitDialog(getString(resid));
}
@Override
public Dialog showWaitDialog(String text) {
if (mWaitDialog == null) {
mWaitDialog = DialogManager.getWaitDialog(this, text);
}
if (mWaitDialog != null) {
TextView textView = mWaitDialog.findViewById(R.id.tv_message);
textView.setText(text);
mWaitDialog.show();
}
return mWaitDialog;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
}
@Override
public void onRationaleAccepted(int requestCode) {
}
@Override
public void onRationaleDenied(int requestCode) {
}
public class BaseAppBarCompat extends AppBarConfig.AppBarCompat {
TextView textViewTitle;
@SuppressLint("ResourceAsColor")
@Override
public void actionbar(@NonNull ActionBar actionbar) {
super.actionbar(actionbar);
// Configure the "Action Bar" here
// and set a common loading method to suit different needs
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.action_bar_layout, null);
((TextView) (view.findViewById(R.id.tv_action_bar_title))).setText(setActionBarTitle() == null ? " " : setActionBarTitle());
textViewTitle = view.findViewById(R.id.tv_action_bar_title);
textViewTitle.setTextSize(setActionBarTitleSize());
view.findViewById(R.id.iv_action_bar_back).setOnClickListener(v -> actionBarBackOnClick());
TextView button = view.findViewById(R.id.btn_action_bar_title_on_right);
button.setVisibility(setActionBarRightVisibility());
button.setText(setActionBarRightText());
view.findViewById(R.id.btn_action_bar_title_on_right).setOnClickListener(v -> actionBarRightOnClick());
// ImageView imageView = view.findViewById(R.id.search_bar);
// imageView.setVisibility(setActionBarRightSearchVisibility());
// imageView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// actionBarRightSearchOnClick();
// }
// });
// padding view
actionbar.setCustomView(view, lp);
// setting params
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
}
public void setTextViewTitle(String title) {
textViewTitle.setText(title);
}
}
protected int setActionBarRightVisibility() {
return View.GONE;
}
protected float setActionBarTitleSize() {
return 22f;
}
// protected int setActionBarRightSearchVisibility() {
// return View.GONE;
//