Android框架组件 ViewBinding详解及使用

初识ViewBinding

顾名思义ViewBinding的意思就是如何将view代码绑定在一块儿。因此其主要解决如何安全优雅地从代码中引用到XML layout文件中的view控件的问题。直到目前为止,Android构建用户界面的主流方式仍然是使用XML格式的layout文件。java

视图访问的方式有经常使用的findViewById,ButterKnife等多种方式,这些方式的各方面对好比下android

谷歌在Android Studio 3.6 Canary 11版本中正式推出视图绑定View Binding安全

首先须要使用AS 3.6 Canary 11之上的版本,这里我使用AS 3.6.1升级gradle plugin版本到3.6.1markdown

ViewBinding是一项功能,使您能够更轻松地编写与视图交互的代码。在模块中启用视图绑定后,它将为该模块中存在的每一个XML布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具备ID的全部视图的直接引用。在大多数状况下,视图绑定替换findViewById()app

环境要求

  • Android Studio版本3.6及以上
  • Gradle 插件版本3.6.0及以上

使用步骤

  • 视图绑定功能可按模块启用。要在某个模块中启用视图绑定,请将 viewBinding 元素添加到其 build.gradle 文件中,以下例所示:
android {
    ...
    
    viewBinding{
        enabled = true
    }
}
复制代码
  • 若是您但愿在生成绑定类时忽略某个布局文件,请将 tools:viewBindingIgnore="true" 属性添加到相应布局文件的根视图中:
<LinearLayout
            ...
            tools:viewBindingIgnore="true" >
        ...
    </LinearLayout>
    
复制代码

为某个模块启用视图绑定功能后,系统会为该模块中包含的每一个 XML 布局文件各生成一个绑定类。每一个绑定类均包含对根视图以及具备 ID 的全部视图的引用。系统会经过如下方式生成绑定类的名称:将 XML 文件的名称转换为驼峰式大小写,并在末尾添加“Binding”一词。ide

  • 假设咱们有一个xml布局文件,命名为activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        />
            
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
复制代码

生成的绑定类将名为 ActivityMainBinding。此类具备两个字段:一个是名为 tvTextTextView,另外一个是名为 btnButton。该布局中的 ImageView 没有 ID,所以绑定类中不存在对它的引用。布局

每一个绑定类还包含一个 getRoot() 方法,用于为相应布局文件的根视图提供直接引用。在此示例中,ActivityMainBinding 类中的 getRoot() 方法会返回 LinearLayout 根视图。gradle

  • 要获取生成的绑定类的实例,您能够调用其静态 inflate() 方法。一般状况下,还能够调用 setContentView(),从而将该绑定类的根视图做为参数进行传递,以使它成为屏幕上的活动视图。在此示例中,您能够在 Activity 中调用 ActivityMainBinding.inflate()
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        mBinding.tvText.setText("是否是这样使用呢?");
        mBinding.btn.setText("我是一个按钮");

    }
}
复制代码
  • 效果

与使用 findViewById 相比,视图绑定具备一些很显著的优势:

  • Null 安全:因为视图绑定会建立对视图的直接引用,所以不存在因视图 ID 无效而引起 Null 指针异常的风险。此外,若是视图仅出如今布局的某些配置中,则绑定类中包含其引用的字段会使用 @Nullable 标记。
  • 类型安全:每一个绑定类中的字段均具备与它们在 XML 文件中引用的视图相匹配的类型。这意味着不存在发生类转换异常的风险。

原理

  • 咱们先来看一下咱们使用ViewBinding时生成的ActivityMainBinding类

  • ActivityMainBinding类代码以下:

原理就是Google在那个用来编译的gradle插件中增长了新功能,当某个module开启ViewBinding功能后,编译的时候就去扫描此模块下的layout文件,生成对应的binding类。那些你所熟悉的findViewById操做都是在这个自动生成的类里面呢,以下所示ui

// Generated by view binder compiler. Do not edit!
package com.example.viewbinding.databinding;

public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final LinearLayout rootView;

  @NonNull
  public final Button btn;

  @NonNull
  public final TextView tvText;

  private ActivityMainBinding(@NonNull LinearLayout rootView, @NonNull Button btn, @NonNull TextView tvText) {
    this.rootView = rootView;
    this.btn = btn;
    this.tvText = tvText;
  }

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

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

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

  @NonNull
  public static ActivityMainBinding 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.
    String missingId;
    missingId: {
      Button btn = rootView.findViewById(R.id.btn);
      if (btn == null) {
        missingId = "btn";
        break missingId;
      }
      TextView tvText = rootView.findViewById(R.id.tv_text);
      if (tvText == null) {
        missingId = "tvText";
        break missingId;
      }
      return new ActivityMainBinding((LinearLayout) rootView, btn, tvText);
    }
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

复制代码

其中核心代码是bind(@NonNull View rootView)方法,除此以外还有两个inflate()重载方法,通常状况下咱们使用这两个方法得到binding类的实例,这些方法都是public static的,经过bind(@NonNull View rootView)这个方法应该能够实现延迟绑定,可是其使用场景应该不多。this

相关文章
相关标签/搜索