Android Viewbinding简介

273 阅读1分钟

一、官方概览

1.1ViewBinding 视图绑定:developer.android.com/topic/libra…

1.2DataBinding(后面会专门讲) 双向绑定:developer.android.com/topic/libra…

二、设计目的 2.1与同类型相比优缺点

image.png

摘自:www.youtube.com/watch?v=Qxj… 2.2设计模式 单例模式-Holder模式 三、原理分析 3.1遇到的小坑

  1. gradle版本号
  2. 依赖于第三方的gradle插件的支持情况
  3. Android Studio的不同版本对应的不同语法
  4. 关于自定义view的视图无法撑开的情况 3.2分析binding文件
public final class ActivityGaImagePickerBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final RecyclerView rippleFolderRV;

  @NonNull
  public final TextView rippleFolderShape;

  @NonNull
  public final RecyclerView rippleImageRV;

  @NonNull
  public final Toolbar toolbar;

  @NonNull
  public final TextView toolbarCenterTitle;

  @NonNull
  public final TextView toolbarLeftTitle;

  @NonNull
  public final TextView toolbarRightTitle;

  private ActivityGaImagePickerBinding(@NonNull ConstraintLayout rootView,
      @NonNull RecyclerView rippleFolderRV, @NonNull TextView rippleFolderShape,
      @NonNull RecyclerView rippleImageRV, @NonNull Toolbar toolbar,
      @NonNull TextView toolbarCenterTitle, @NonNull TextView toolbarLeftTitle,
      @NonNull TextView toolbarRightTitle) {
    this.rootView = rootView;
    this.rippleFolderRV = rippleFolderRV;
    this.rippleFolderShape = rippleFolderShape;
    this.rippleImageRV = rippleImageRV;
    this.toolbar = toolbar;
    this.toolbarCenterTitle = toolbarCenterTitle;
    this.toolbarLeftTitle = toolbarLeftTitle;
    this.toolbarRightTitle = toolbarRightTitle;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityGaImagePickerBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityGaImagePickerBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_ga_image_picker, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityGaImagePickerBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.rippleFolderRV;
      RecyclerView rippleFolderRV = rootView.findViewById(id);
      if (rippleFolderRV == null) {
        break missingId;
      }

      id = R.id.rippleFolderShape;
      TextView rippleFolderShape = rootView.findViewById(id);
      if (rippleFolderShape == null) {
        break missingId;
      }

      id = R.id.rippleImageRV;
      RecyclerView rippleImageRV = rootView.findViewById(id);
      if (rippleImageRV == null) {
        break missingId;
      }

      id = R.id.toolbar;
      Toolbar toolbar = rootView.findViewById(id);
      if (toolbar == null) {
        break missingId;
      }

      id = R.id.toolbarCenterTitle;
      TextView toolbarCenterTitle = rootView.findViewById(id);
      if (toolbarCenterTitle == null) {
        break missingId;
      }

      id = R.id.toolbarLeftTitle;
      TextView toolbarLeftTitle = rootView.findViewById(id);
      if (toolbarLeftTitle == null) {
        break missingId;
      }

      id = R.id.toolbarRightTitle;
      TextView toolbarRightTitle = rootView.findViewById(id);
      if (toolbarRightTitle == null) {
        break missingId;
      }

      return new ActivityGaImagePickerBinding((ConstraintLayout) rootView, rippleFolderRV,
          rippleFolderShape, rippleImageRV, toolbar, toolbarCenterTitle, toolbarLeftTitle,
          toolbarRightTitle);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

3.3binding文件的生成时机 3.3.1 AS3.6之前 通过注解处理器生成,build工程即可 3.3.2 AS3.6 之后

image.png

LayoutXmlProcessor.java

APT,AST