当前位置:   article > 正文

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

include databinding

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

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

  1. android {
  2. dataBinding {
  3. enabled = true
  4. }
  5. …………………………
  6. }

2.标题栏xml布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto">
  4. <android.support.constraint.ConstraintLayout
  5. android:id="@+id/title_bar"
  6. android:layout_width="match_parent"
  7. android:layout_height="48dp"
  8. android:background="@color/black">
  9. <TextView
  10. android:id="@+id/tv_public_back"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentStart="true"
  14. android:layout_centerVertical="true"
  15. android:layout_marginLeft="10dp"
  16. android:drawableLeft="@mipmap/back"
  17. android:drawablePadding="5dp"
  18. android:onClick="@{toolbar.clickBack}"
  19. android:padding="4dp"
  20. android:text="@{toolbar.back}"
  21. android:textColor="@color/white"
  22. android:textSize="16sp"
  23. app:layout_constraintBottom_toBottomOf="parent"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toTopOf="parent" />
  26. <TextView
  27. android:id="@+id/tv_public_title"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_centerInParent="true"
  31. android:layout_centerHorizontal="true"
  32. android:layout_centerVertical="true"
  33. android:onClick="@{toolbar.clickTitle}"
  34. android:text="@{toolbar.tvTitle}"
  35. android:textColor="@color/white"
  36. android:textSize="18sp"
  37. app:layout_constraintBottom_toBottomOf="parent"
  38. app:layout_constraintEnd_toEndOf="parent"
  39. app:layout_constraintStart_toStartOf="parent"
  40. app:layout_constraintTop_toTopOf="parent" />
  41. <TextView
  42. android:id="@+id/tv_public_right"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_alignParentEnd="true"
  46. android:layout_centerVertical="true"
  47. android:layout_marginRight="10dp"
  48. android:onClick="@{toolbar.clickRight}"
  49. android:text="@{toolbar.tvRight}"
  50. android:textColor="@color/white"
  51. app:layout_constraintBottom_toBottomOf="parent"
  52. app:layout_constraintEnd_toEndOf="parent"
  53. app:layout_constraintTop_toTopOf="parent" />
  54. </android.support.constraint.ConstraintLayout>
  55. <data>
  56. <variable
  57. name="toolbar"
  58. type="com.example.module_common.common.toolbar.TitleBuilder" />
  59. </data>
  60. </layout>

3.标题栏的封装类TitleBuilder

  1. public class TitleBuilder {
  2. private String back;
  3. private String tvTitle;
  4. private String tvRight;
  5. //三个接口是为了重写点击事件
  6. private BackClickListener backClickListener;
  7. private TitleClickListener titleClickListener;
  8. private RightClickListener rightClickListener;
  9. public static TitleBuilder getActivityToolBar(Activity activity) {
  10. String back = activity.getString(R.string.public_back);//返回
  11. TitleBuilder bar = new TitleBuilder();
  12. bar.backClickListener = new BackClickListener() {
  13. @Override
  14. public void clickBack(View view) {
  15. ActivityMgr.getInstance().finishActivity(activity);//关闭当前activity,使用activity管理类
  16. }
  17. };
  18. bar.titleClickListener = new TitleClickListener() {
  19. @Override
  20. public void clickTitle(View view) {
  21. }
  22. };
  23. bar.rightClickListener = new RightClickListener() {
  24. @Override
  25. public void clickRight(View view) {
  26. }
  27. };
  28. bar.setBack(back);
  29. return bar;
  30. }
  31. public void clickBack(View view) {
  32. if (null != this.backClickListener) {
  33. this.backClickListener.clickBack(view);
  34. }
  35. }
  36. public void clickTitle(View view) {
  37. if (null != this.titleClickListener) {
  38. this.titleClickListener.clickTitle(view);
  39. }
  40. }
  41. public void clickRight(View view) {
  42. if (null != this.rightClickListener) {
  43. this.rightClickListener.clickRight(view);
  44. }
  45. }
  46. public interface BackClickListener {
  47. void clickBack(View view);
  48. }
  49. public interface TitleClickListener {
  50. void clickTitle(View view);
  51. }
  52. public interface RightClickListener {
  53. void clickRight(View view);
  54. }
  55. public String getBack() {
  56. return back;
  57. }
  58. public void setBack(String back) {
  59. this.back = back;
  60. }
  61. public String getTvTitle() {
  62. return tvTitle;
  63. }
  64. public void setTvTitle(String tvTitle) {
  65. this.tvTitle = tvTitle;
  66. }
  67. public String getTvRight() {
  68. return tvRight;
  69. }
  70. public void setTvRight(String tvRight) {
  71. this.tvRight = tvRight;
  72. }
  73. public BackClickListener getBackClickListener() {
  74. return backClickListener;
  75. }
  76. public void setBackClickListener(BackClickListener backClickListener) {
  77. this.backClickListener = backClickListener;
  78. }
  79. public TitleClickListener getTitleClickListener() {
  80. return titleClickListener;
  81. }
  82. public void setTitleClickListener(TitleClickListener titleClickListener) {
  83. this.titleClickListener = titleClickListener;
  84. }
  85. public RightClickListener getRightClickListener() {
  86. return rightClickListener;
  87. }
  88. public void setRightClickListener(RightClickListener rightClickListener) {
  89. this.rightClickListener = rightClickListener;
  90. }
  91. }

4.BaseActivity

  1. public class BaseActivity extends AppCompatActivity {
  2. private TitleBuilder toolbar;
  3. @Override
  4. protected void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. ActivityMgr.getInstance().addActivity(this);//加入到堆栈,activity管理类
  7. toolbar = TitleBuilder.getActivityToolBar(this);
  8. }
  9. @Override
  10. public boolean onKeyDown(int keyCode, KeyEvent event) {//重写返回(只对物理返回键)
  11. if (keyCode == KEYCODE_BACK) {
  12. ActivityMgr.getInstance().finishActivity(this);//关闭当前activity,activity管理类
  13. }
  14. return super.onKeyDown(keyCode, event);
  15. }
  16. public TitleBuilder getToolbar() {
  17. return toolbar;
  18. }
  19. public void setToolbar(TitleBuilder toolbar) {
  20. this.toolbar = toolbar;
  21. }
  22. }

5.activity_show_tool_bar布局:

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

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

toolbar是标题栏布局public_title里的<variable  name="toolbar" type="**"/>这里要对应上,

bar就是这个页面定义的<variable  name="bar" type="**"/>,可以随意改,但是要对应上

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools">
  5. <android.support.constraint.ConstraintLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".ui.activity.ShowToolBarActivity">
  9. <TextView
  10. android:id="@+id/textView2"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="展示公共标题栏ToolBar"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toBottomOf="@+id/bar" />
  18. <include
  19. android:id="@+id/bar"
  20. layout="@layout/public_title"
  21. app:toolbar="@{bar}" />
  22. <!--toolbar是标题栏布局public_title里的<variable name="toolbar" type="**"/>这里要对应上,bar就是这个页面定义的<variable name="bar" type="**"/>,可以随意改,但是要对应上-->
  23. </android.support.constraint.ConstraintLayout>
  24. <data>
  25. <variable
  26. name="bar"
  27. type="com.example.module_common.common.toolbar.TitleBuilder" />
  28. </data>
  29. </layout>

6.ShowToolBarActivity必须继承BaseActivity

  1. public class ShowToolBarActivity extends BaseActivity {
  2. private ActivityShowToolBarBinding mBinding;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. mBinding = DataBindingUtil.setContentView(this, R.layout.activity_show_tool_bar);
  7. mBinding.setBar(getToolbar());
  8. initViwe();
  9. }
  10. private void initViwe() {
  11. getToolbar().setBack("不叫返回叫週莫");
  12. getToolbar().setTvTitle("週莫标题");
  13. getToolbar().setTvRight("週莫右边");
  14. //重写三个事件
  15. getToolbar().setBackClickListener(new TitleBuilder.BackClickListener() {
  16. @Override
  17. public void clickBack(View view) {
  18. Toast.makeText(ShowToolBarActivity.this, "我不想关闭,我想留着", Toast.LENGTH_SHORT).show();
  19. }
  20. });
  21. getToolbar().setTitleClickListener(new TitleBuilder.TitleClickListener() {
  22. @Override
  23. public void clickTitle(View view) {
  24. Toast.makeText(ShowToolBarActivity.this, "你点击了週莫标题", Toast.LENGTH_SHORT).show();
  25. }
  26. });
  27. getToolbar().setRightClickListener(new TitleBuilder.RightClickListener() {
  28. @Override
  29. public void clickRight(View view) {
  30. Toast.makeText(ShowToolBarActivity.this, "你点击了週莫右边", Toast.LENGTH_SHORT).show();
  31. }
  32. });
  33. }
  34. }

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

效果图如下:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/320676
推荐阅读
相关标签
  

闽ICP备14008679号