AndroidパッケージBaseActivity

3376 ワード

title:AndroidパッケージBaseActivity date 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のパッケージ~