databinding使用include完美封装标题栏,扩展性强

接下来跟我一起动手封装标题栏,废话不多说,走起!!!

1.build.gradle下添加(哪个module需databinding就在对应的build.gradle下添加):

android {
    dataBinding {
        enabled = true
    }
…………………………
}

2.标题栏xml布局如下:





    

        

        

        


    

    

        

    

3.标题栏的封装类TitleBuilder

public class TitleBuilder {
    private String back;
    private String tvTitle;
    private String tvRight;
    //三个接口是为了重写点击事件
    private BackClickListener backClickListener;
    private TitleClickListener titleClickListener;
    private RightClickListener rightClickListener;

    public static TitleBuilder getActivityToolBar(Activity activity) {
        String back = activity.getString(R.string.public_back);//返回
        TitleBuilder bar = new TitleBuilder();
        bar.backClickListener = new BackClickListener() {
            @Override
            public void clickBack(View view) {
                ActivityMgr.getInstance().finishActivity(activity);//关闭当前activity,使用activity管理类
            }
        };

        bar.titleClickListener = new TitleClickListener() {
            @Override
            public void clickTitle(View view) {

            }
        };

        bar.rightClickListener = new RightClickListener() {
            @Override
            public void clickRight(View view) {

            }
        };
        bar.setBack(back);
        return bar;
    }

    public void clickBack(View view) {
        if (null != this.backClickListener) {
            this.backClickListener.clickBack(view);
        }
    }

    public void clickTitle(View view) {
        if (null != this.titleClickListener) {
            this.titleClickListener.clickTitle(view);
        }
    }

    public void clickRight(View view) {
        if (null != this.rightClickListener) {
            this.rightClickListener.clickRight(view);
        }

    }


    public interface BackClickListener {
        void clickBack(View view);
    }

    public interface TitleClickListener {
        void clickTitle(View view);
    }

    public interface RightClickListener {
        void clickRight(View view);
    }

    public String getBack() {
        return back;
    }

    public void setBack(String back) {
        this.back = back;
    }

    public String getTvTitle() {
        return tvTitle;
    }

    public void setTvTitle(String tvTitle) {
        this.tvTitle = tvTitle;
    }

    public String getTvRight() {
        return tvRight;
    }

    public void setTvRight(String tvRight) {
        this.tvRight = tvRight;
    }

    public BackClickListener getBackClickListener() {
        return backClickListener;
    }

    public void setBackClickListener(BackClickListener backClickListener) {
        this.backClickListener = backClickListener;
    }

    public TitleClickListener getTitleClickListener() {
        return titleClickListener;
    }

    public void setTitleClickListener(TitleClickListener titleClickListener) {
        this.titleClickListener = titleClickListener;
    }

    public RightClickListener getRightClickListener() {
        return rightClickListener;
    }

    public void setRightClickListener(RightClickListener rightClickListener) {
        this.rightClickListener = rightClickListener;
    }
}

4.BaseActivity

public class BaseActivity extends AppCompatActivity {
    private TitleBuilder toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMgr.getInstance().addActivity(this);//加入到堆栈,activity管理类

        toolbar = TitleBuilder.getActivityToolBar(this);
    }

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {//重写返回(只对物理返回键)
        if (keyCode == KEYCODE_BACK) {
            ActivityMgr.getInstance().finishActivity(this);//关闭当前activity,activity管理类
        }
        return super.onKeyDown(keyCode, event);
    }

    public TitleBuilder getToolbar() {
        return toolbar;
    }

    public void setToolbar(TitleBuilder toolbar) {
        this.toolbar = toolbar;
    }
}

5.activity_show_tool_bar布局:

注(担心有些朋友没有注意看布局中的提示,这里单独拿出来提示一下):  

            android:id="@+id/bar"
            layout="@layout/public_title"
            app:toolbar="@{bar}" />

toolbar是标题栏布局public_title里的这里要对应上,

bar就是这个页面定义的,可以随意改,但是要对应上





    

        

        
        

    

    

        

    

6.ShowToolBarActivity必须继承BaseActivity

public class ShowToolBarActivity extends BaseActivity {
    private ActivityShowToolBarBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_show_tool_bar);
        mBinding.setBar(getToolbar());

        initViwe();
    }

    private void initViwe() {
        getToolbar().setBack("不叫返回叫週莫");
        getToolbar().setTvTitle("週莫标题");
        getToolbar().setTvRight("週莫右边");

        //重写三个事件
        getToolbar().setBackClickListener(new TitleBuilder.BackClickListener() {
            @Override
            public void clickBack(View view) {
                Toast.makeText(ShowToolBarActivity.this, "我不想关闭,我想留着", Toast.LENGTH_SHORT).show();
            }
        });

        getToolbar().setTitleClickListener(new TitleBuilder.TitleClickListener() {
            @Override
            public void clickTitle(View view) {
                Toast.makeText(ShowToolBarActivity.this, "你点击了週莫标题", Toast.LENGTH_SHORT).show();
            }
        });

        getToolbar().setRightClickListener(new TitleBuilder.RightClickListener() {
            @Override
            public void clickRight(View view) {
                Toast.makeText(ShowToolBarActivity.this, "你点击了週莫右边", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

7.一气呵成,收工。有不足之处,请评论区指出,谢谢!

效果图如下:

databinding使用include完美封装标题栏,扩展性强_第1张图片

 

你可能感兴趣的:(AS修炼手册)