ToolBar封裝策略

TheShy_發表於2019-03-04

效果:

ToolBar封裝策略

策略一: 使用原生toolbar進行封裝

流程:

* 1所有的類都繼承此AppToolBarActivity.

* 2打氣筒載入一個佈局到根佈局(即layout_toolbar),此佈局包含ToolBar+Framelayout.

* 3getContentView()為抽象方法,返回一個View 將此View add到framelayout中.

* 4做一些ToolBar的初始化操作.

* 5使用
複製程式碼
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_toolbar, (ViewGroup) getWindow().getDecorView().getRootView(), false);
        mContent_frame = view.findViewById(R.id.content_frame);
        if(getContentView() != null){
            mContent_frame.addView(getContentView());
        }
        setContentView(view);

        ButterKnife.bind(this);
        steepTitle();
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        setTitle(getTitle());

        initWidget();
        initData();
    }
複製程式碼
  • 處理ToolBar的返回事件:
    //style.xml中
    <item name="android:homeAsUpIndicator">@mipmap/back_white</item>
    //AppToolBarActivity中
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
複製程式碼
  • 處理中間Title 重寫setTitle()
    //bar 中間標題
    @Override
    public void setTitle(CharSequence title) {
        mToolbar_tv_title.setText(title);
        mRadioGroup.setVisibility(View.GONE);
    }   
複製程式碼
  • 處理右側可能是文字或圖片按鈕 中間可能是多按鈕
    //bar右側是文字
    protected void setRightTextButtonEnable(@StringRes int rid, View.OnClickListener onClickListener) {
        mTv_title_right.setText(rid);
        mTv_title_right.setOnClickListener(onClickListener);
        mTv_title_right.setVisibility(View.VISIBLE);
    }
    
    //bar右側是圖示
    protected void setRightImageButtonEnable(@DrawableRes int rid, View.OnClickListener onClickListener) {
        mIv_title_right.setImageResource(rid);
        mIv_title_right.setOnClickListener(onClickListener);
        mIv_title_right.setVisibility(View.VISIBLE);
        mTv_title_right.setVisibility(View.GONE);

    }

    //bar中間是多按鈕
    protected void setCenterRadioGroupEnable(String btnLefttext,String btnRighttext, RadioGroup.OnCheckedChangeListener onClickListener) {
        mRadioBtnLeft.setText(btnLefttext);
        mRadioBtnRight.setText(btnRighttext);
        mRadioGroup.setOnCheckedChangeListener(onClickListener);
        mRadioGroup.setVisibility(View.VISIBLE);
        mToolbar_tv_title.setVisibility(View.GONE);
    }
    
複製程式碼
  • 處理所謂的沉浸式
    //載入沉浸式狀態列
    public void steepTitle() {
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //注意要清除 FLAG_TRANSLUCENT_STATUS flag
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.maincolor));
        }
    }
   
複製程式碼
  • 最後使用超簡單:
    @Override
    protected View getContentView() {
        return initRId(R.layout.activity_main);
    }

    @Override
    protected void initWidget() {
        setBackEnable(false);
        setTitle("主頁");
    }
複製程式碼

github地址傳送門

策略2:自己寫一個Bar 使用過載方法初始化

  • 核心程式碼:
    protected void initAppBar() {
        initAppBar(true, false, -1, -1);
    }

    protected void initAppBar(boolean isBack) {
        initAppBar(isBack, false, -1, -1);
    }

    protected void initAppBar(boolean isBack, boolean isRightText) {
        initAppBar(isBack, isRightText, -1, -1);
    }

    protected void initAppBar(boolean isBack, boolean isRightText, @ColorRes int bgColor, @ColorRes int textColor) {
        //動態新增appbar 這樣無需在每個xml中includ進去appbar的佈局
        //如果不寫這四行 需要在每個xml中incloud進去bar佈局
        ViewGroup view = getWindow().getDecorView().findViewById(android.R.id.content);
        ViewGroup inflate = (ViewGroup) view.getChildAt(0);
        View barView = getLayoutInflater().inflate(R.layout.common_appbar, inflate,false);
        inflate.addView(barView,0);

        RelativeLayout layout = findViewById(R.id.common_appbar_rl);
        if (layout == null) {
            return;
        }
        invadeStatusBar();
        if (bgColor != -1) {
            layout.setBackgroundResource(bgColor);
        }
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);
        } else {
            layoutParams.setMargins(0, 0, 0, 0);
        }
        layout.setLayoutParams(layoutParams);
        ImageView iconIV = findViewById(R.id.common_appbar_iv);
        if (!isBack) {
            iconIV.setVisibility(View.GONE);
        } else {
            iconIV.setVisibility(View.VISIBLE);
            iconIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        TextView centerTV = findViewById(R.id.common_appbar_center_tv);
        if (textColor != -1) {
            centerTV.setTextColor(getResources().getColor(textColor));
        }
        centerTV.setText(TypeUtil.isBlank(setAppBarTitle()) ? "" : setAppBarTitle());
        TextView rightTV = findViewById(R.id.common_appbar_right_tv);
        if (!isRightText) {
            rightTV.setVisibility(View.GONE);
        } else {
            rightTV.setVisibility(View.VISIBLE);
            if (textColor != -1) {
                centerTV.setTextColor(getResources().getColor(textColor));
            }
            rightTV.setText(TypeUtil.isBlank(setAppBarRightTitle()) ? "" : setAppBarRightTitle());
            rightTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onAppBarRightClick();
                }
            });
        }
    }
複製程式碼
  • 過載方法:
    protected abstract String setAppBarTitle();

    protected abstract String setAppBarRightTitle();

    protected abstract void onAppBarRightClick();
複製程式碼
  • 使用:
  initAppBar(true,true);

  @Override
    protected String setAppBarTitle() {
        return "Tactics2";
    }

    @Override
    protected String setAppBarRightTitle() {
        return "更多";
    }

    @Override
    protected void onAppBarRightClick() {
        ToastUtil.showToast("更多");
    }
複製程式碼

最後安利一個小技巧:

  • Activity跳轉時候,可以這麼寫,更舒服:
    //只需要在目標Activity敲下start 就會出現自帶的方法,可以傳遞一些資料:
    public static void start(Context context, String id) {
        Intent starter = new Intent(context, TacticsOneActivity.class);
        starter.putExtra("id", id);
        context.startActivity(starter);
    }

    //在執行跳轉的某按鈕處,只需簡短一句:
    XxxActivity.start(this,"1");
複製程式碼
  • 同理Fragment:
    //只需要在目標Fragment敲下newI :
    public static ChargeRecordFragment newInstance(int type,String coinName,String coinDetailName) {
        Bundle args = new Bundle();
        args.putInt("type",type);
        args.putString("coinName",coinName);
        args.putString("coinDetailName",coinDetailName);
        ChargeRecordFragment fragment = new ChargeRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }
    //Activity的某處拿到例項並傳遞資料:
    ChargeRecordFragment.newInstance(0,mCoinName,mCoinDetailName);
複製程式碼

相關文章