Android封装BaseActivity


title: Android封装BaseActivity
date: 2017-11-25 15:23:26
tags: Activity
categories: Android


Android封装BaseActivity

Activity在我们的日常开发中应该是最为常用的组件了,基础的使用大家肯定都是会用的,但是有的时候我们需要把它封装一下,功能大概如下:

  • 抽象出公共方法,增强代码复用的逻辑
  • 统一管理ActionBar
  • 统一管理Toast,Log,Dialog

直接上代码吧,好像没什么可说的了

//BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity implements DialogControl, BaseViewInterface {

    private boolean _isVisible = false;
    protected LayoutInflater mInflater;
    protected ViewGroup mActionBar;
    private LoadingUtils loading;
    private String mActionBarTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentLayout();
        if (getLayoutId() != 0) {
            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            if (hasBackButton()) {
                mActionBar = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null);
                TextView tv = (TextView) mActionBar.findViewById(R.id.action_bar_title);

                tv.setText(getActionBarTitle());
                mActionBar.findViewById(R.id.action_bar_back).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e("lin", "---lin--->  111111111111111111111111");
                        finish();
                    }
                });
                layout.addView(mActionBar);
            }
            View view = LayoutInflater.from(this).inflate(getLayoutId(), null);
            layout.addView(view);
            setContentView(layout);
        }
        ButterKnife.bind(this);
        init(savedInstanceState);
        initData();
        initViews();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void onBeforeSetContentLayout() {
    }

    protected int getLayoutId() {
        return 0;
    }

    protected View inflateView(int resId) {
        return mInflater.inflate(resId, null);
    }

    protected String getActionBarTitle() {
        return mActionBarTitle;
    }

    protected boolean hasBackButton() {
        return false;
    }

    protected boolean hasActionBar() {
        return false;
    }

    protected void init(Bundle savedInstanceState) {
    }



    @Override
    public void showWaitDialog() {
        if (_isVisible) {
            return;
        }
        _isVisible = true;
        loading = new LoadingUtils(this).setMessage("正在加载,请稍后...");
        loading.show();
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && loading != null) {
            _isVisible = false;
            loading.dismiss();

        }
    }
}

//DialogControl.java
public interface DialogControl {

    public abstract void showWaitDialog();

    public abstract void hideWaitDialog();

}
//BaseViewInterface.java
public interface BaseViewInterface {

    void initViews();

    void initData();

}

以上便是对Activity的封装~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,539评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,751评论 0 17
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 1,483评论 0 5
  • 今天听了牛校长的《他山之石。可以攻玉》的报告,使我又一次心灵得到了洗礼,字字有力,句句经典,层层递进,她的报告内涵...
    王者风范1阅读 257评论 0 0
  • 遇到了初曦,改变了我的人生轨迹。 接触初曦是在2016的年末,我的学生像我推荐的,她说老师您的脸上好多斑点,感觉皮...
    君朋视觉阅读 7,185评论 0 1