美文网首页安卓封装
Android 小技巧之 BaseActivity、BaseFr

Android 小技巧之 BaseActivity、BaseFr

作者: Kevin_小飞象 | 来源:发表于2021-06-10 14:40 被阅读0次
    每日一图.jpg
    1. BaseActivity.java
    public abstract class BaseActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setStatusBar();
    
            View view = LayoutInflater.from(this).inflate(getLayoutId(), null);
            setContentView(view);
            ButterKnife.bind(this);
    
            initView();
        }
    
        private void setStatusBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (isUseFullScreenMode()) {
                    StatusBarUtil.transparencyBar(this);
                } else {
                    StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
                }
    
                if (isUseBlackFontWithStatusBar()) {
                    StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
                }
            }
        }
    
        /**
         * 是否设置成透明状态栏,即就是全屏模式
         */
        protected boolean isUseFullScreenMode() {
            return false;
        }
    
        /**
         * 更改状态栏颜色,只有非全屏模式下有效
         */
        protected int getStatusBarColor() {
            return R.color.purple_700;
        }
    
        /**
         * 是否改变状态栏文字颜色为黑色,默认为黑色
         */
        protected boolean isUseBlackFontWithStatusBar() {
            return true;
        }
    
        /**
         * 不带参数的跳转
         *
         * @param clazz 跳转到的目标类
         */
        protected void readyGo(final Class<?> clazz) {
            Intent intent = new Intent(this, clazz);
            startActivity(intent);
        }
    
        /**
         * 带参数的跳转
         *
         * @param clazz  跳转到的目标类
         * @param bundle 参数
         */
        protected void readyGo(final Class<?> clazz, final Bundle bundle) {
            Intent intent = new Intent(this, clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }
    
        /**
         * 跳转且返回结果
         *
         * @param clazz       跳转到的目标类
         * @param requestCode 请求码
         */
        protected void readyGoForResult(final Class<?> clazz, final int requestCode) {
            Intent intent = new Intent(this, clazz);
            startActivityForResult(intent, requestCode);
        }
    
        /**
         * 带参数跳转且返回结果
         *
         * @param clazz       跳转到的目标类
         * @param requestCode 请求码
         * @param bundle      参数
         */
        protected void readyGoForResult(final Class<?> clazz, final int requestCode, final Bundle bundle) {
            Intent intent = new Intent(this, clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requestCode);
        }
    
        protected abstract int getLayoutId();
    
    
        protected abstract void initView();
    
    
    }
    
    1. BaseFragment.java
    public abstract class BaseFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(layoutId(),container,false);
            ButterKnife.bind(this,view);
            initData();
            initView(view);
            return view;
    
        }
    
        /**
         * 初始化布局
         * @return 布局id
         */
        protected abstract int layoutId();
    
        /**
         * 初始化控件
         * @param view 布局view
         */
        protected abstract void initView(View view);
    
        /**
         * 初始化,绑定数据
         */
        protected abstract void initData();
    
        /**
         * 不带参数的跳转
         *
         * @param clazz 跳转到的目标类
         */
        protected void readyGo(final Class<?> clazz) {
            Intent intent = new Intent(getActivity(), clazz);
            startActivity(intent);
        }
    
        /**
         * 带参数的跳转
         *
         * @param clazz  跳转到的目标类
         * @param bundle 参数
         */
        protected void readyGo(final Class<?> clazz, final Bundle bundle) {
            Intent intent = new Intent(getActivity(), clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }
    
        /**
         * 跳转且返回结果
         *
         * @param clazz       跳转到的目标类
         * @param requestCode 请求码
         */
        protected void readyGoForResult(final Class<?> clazz, final int requestCode) {
            Intent intent = new Intent(getActivity(), clazz);
            startActivityForResult(intent, requestCode);
        }
    
        /**
         * 带参数跳转且返回结果
         *
         * @param clazz       跳转到的目标类
         * @param requestCode 请求码
         * @param bundle      参数
         */
        protected void readyGoForResult(final Class<?> clazz, final int requestCode, final Bundle bundle) {
            Intent intent = new Intent(getActivity(), clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requestCode);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    }
    
    
    1. BaseDialog.java
    public class BaseDialog extends Dialog {
        private Window mWindow;
    
        public BaseDialog(@NonNull Context context) {
            super(context);
        }
    
        public BaseDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
        }
    
        protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }
    
        public void setView(View view) {
            setContentView(view);
        }
    
        public void setView(int ID) {
            setContentView(ID);
        }
    
        public void setProperty(int x,int y,int w,int h) {
            mWindow = getWindow();
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.x = x;  //设置对话框的位置,0为中间
            params.y = y;
            params.width = w;
            params.height = h;
            params.alpha = 1.0f; //设置对话框的透明度,1f不透明
            params.gravity = Gravity.CENTER; //设置显示在中间
            mWindow.setAttributes(params);
        }
    }
    

    相关文章

      网友评论

        本文标题:Android 小技巧之 BaseActivity、BaseFr

        本文链接:https://www.haomeiwen.com/subject/cafzsltx.html