[关闭]
@zifeng328573112 2021-03-16T08:37:50.000000Z 字数 3973 阅读 159

ViewBinding技术

1. ViewBinding视图绑定

在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。通过视图绑定功能,可以替代findViewById,从而更轻松地编写可与视图交互的代码。

2. 使用条件

视图绑定在 Android Studio 3.6 Canary 11 及更高版本中可用。

3. 开启视图绑定

  1. android {
  2. viewBinding {
  3. enabled = true
  4. }
  5. }

4. 忽略某个布局文件的视图绑定

  1. <LinearLayout
  2. tools:viewBindingIgnore="true" >
  3. </LinearLayout>

5. 生成XML绑定类

为某个模块启用视图绑定功能后,系统会为该模块中包含的每个XML布局文件各生成一个绑定类。每个绑定类均包含对根视图以及具有ID的所有视图的引用。绑定类的名称为:XML文件的名称转换为驼峰式大小写+Binding。

XML文件: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/tv_hello_world"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Hello World!"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintLeft_toLeftOf="parent"
  15. app:layout_constraintRight_toRightOf="parent"
  16. app:layout_constraintTop_toTopOf="parent" />
  17. </androidx.constraintlayout.widget.ConstraintLayout>

自动生成绑定类文件:

img4

  1. ActivityBleBinding.java
  2. public final class ActivityBleBinding implements ViewBinding {
  3. @NonNull
  4. private final RelativeLayout rootView;
  5. @NonNull
  6. public final FloatingActionButton floatingButton;
  7. @NonNull
  8. public final LinearLayout llAdapterTip;
  9. @NonNull
  10. public final RecyclerView recyclerView;
  11. @NonNull
  12. public final SwipeRefreshLayout swipeLayout;
  13. @NonNull
  14. public final TextView tvAdapterStates;
  15. @NonNull
  16. public final TextView tvTip;
  17. private ActivityBleBinding(@NonNull RelativeLayout rootView,
  18. @NonNull FloatingActionButton floatingButton, @NonNull LinearLayout llAdapterTip,
  19. @NonNull RecyclerView recyclerView, @NonNull SwipeRefreshLayout swipeLayout,
  20. @NonNull TextView tvAdapterStates, @NonNull TextView tvTip) {
  21. this.rootView = rootView;
  22. this.floatingButton = floatingButton;
  23. this.llAdapterTip = llAdapterTip;
  24. this.recyclerView = recyclerView;
  25. this.swipeLayout = swipeLayout;
  26. this.tvAdapterStates = tvAdapterStates;
  27. this.tvTip = tvTip;
  28. }
  29. @Override
  30. @NonNull
  31. public RelativeLayout getRoot() {
  32. return rootView;
  33. }
  34. @NonNull
  35. public static ActivityBleBinding inflate(@NonNull LayoutInflater inflater) {
  36. return inflate(inflater, null, false);
  37. }
  38. @NonNull
  39. public static ActivityBleBinding inflate(@NonNull LayoutInflater inflater,
  40. @Nullable ViewGroup parent, boolean attachToParent) {
  41. View root = inflater.inflate(R.layout.activity_ble, parent, false);
  42. if (attachToParent) {
  43. parent.addView(root);
  44. }
  45. return bind(root);
  46. }
  47. @NonNull
  48. public static ActivityBleBinding bind(@NonNull View rootView) {
  49. // The body of this method is generated in a way you would not otherwise write.
  50. // This is done to optimize the compiled bytecode for size and performance.
  51. int id;
  52. missingId: {
  53. id = R.id.floatingButton;
  54. FloatingActionButton floatingButton = rootView.findViewById(id);
  55. if (floatingButton == null) {
  56. break missingId;
  57. }
  58. id = R.id.ll_adapter_tip;
  59. LinearLayout llAdapterTip = rootView.findViewById(id);
  60. if (llAdapterTip == null) {
  61. break missingId;
  62. }
  63. id = R.id.recyclerView;
  64. RecyclerView recyclerView = rootView.findViewById(id);
  65. if (recyclerView == null) {
  66. break missingId;
  67. }
  68. id = R.id.swipeLayout;
  69. SwipeRefreshLayout swipeLayout = rootView.findViewById(id);
  70. if (swipeLayout == null) {
  71. break missingId;
  72. }
  73. id = R.id.tv_adapter_states;
  74. TextView tvAdapterStates = rootView.findViewById(id);
  75. if (tvAdapterStates == null) {
  76. break missingId;
  77. }
  78. id = R.id.tv_tip;
  79. TextView tvTip = rootView.findViewById(id);
  80. if (tvTip == null) {
  81. break missingId;
  82. }
  83. return new ActivityBleBinding((RelativeLayout) rootView, floatingButton, llAdapterTip,
  84. recyclerView, swipeLayout, tvAdapterStates, tvTip);
  85. }
  86. String missingId = rootView.getResources().getResourceName(id);
  87. throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  88. }
  89. }

该类实现了ViewBinding接口,: ViewBinding.java

  1. /** A type which binds the views in a layout XML to fields. */
  2. public interface ViewBinding {
  3. /**
  4. * Returns the outermost {@link View} in the associated layout file. If this binding is for a
  5. * {@code <merge>} layout, this will return the first view inside of the merge tag.
  6. */
  7. @NonNull
  8. View getRoot();
  9. }

6. 使用绑定类文件

img5

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注