android 封装的基础类----BaseActivity

封装的基础类----BaseActivity

需求要点:

  1. 显示进度的dialog;
  2. 自定义Actionbar;
  3. 返回按钮的处理;
  4. 是否显示状态栏;
  5. activity的管理;
  6. 自适应布局控件;

实现:

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;
//   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值