ViewBinding 使用简介

1.背景

在日常app界面开发过程中,经常会遇到如下场景:

  • 场景1:升级到较新版本Android Studio后,在使用R.id.xxxx时会出现警告提示:

    Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them as annotation attributes
    
  • 场景2:在日常UI界面开发过程中,每个界面的View的id命名是一个相对头疼的问题。为了较好的区分每个id,通常id的命名会很长,导致id写起来啰嗦,也缺乏美观。不同的开发人员有各种喜好的命名风格,导致命名上相对混乱。例如类似这种:

    R.id.app_item_game_comment_detail_bottom_user_name
    
  • 场景3:视图变量声明后,使用过程中经常出现空指针异常。当前界面布局文件中没有该id,View声明中确使用了这个id,导致空指针,程序崩溃退出。

  • 场景4:butterknife 已经停止维护了,不再升级更新。不想继续使用 butterknife,但需要其它方法代替

有什么方法可以预防或者减少类似的问题发生呢?答案是肯定的:ViewBinding

2.什么是ViewBinding

View Binding是Android Studio 3.6 推出的新特性,旨在替代findViewById(内部实现还是使用findViewById)。算是一个语法糖。
通过ViewBinding,可以更轻松地编写可与视图交互的代码。在模块中启用ViewBinding之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。

在大多数情况下,ViewBinding可以做到替代 findViewById。

3.ViewBinding怎么使用

启用Viewbinding功能:在模块build.gradle文件android节点下添加如下代码

android {
  ......
  buildFeatures {
      viewBinding true
  }
}

重新编译后系统会为每个布局文件生成对应的Binding类。该类中包含对应布局中具有 ID 的所有视图的直接引用。

文件生成规则:将布局文件名转成驼峰命名并添加以Binding结尾的类。例如activity_main.xml生成的类是ActivityMainBinding.java。类的字段是对应布局文件中的id。字段名生成规则和类名类似。

生成类的目录在模块根目录/build/generated/data_binding_base_class_source_out下。如下图:


生成文件示例

3.1 在Activity中使用

在MainActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="come on" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity中使用:

ActivityMainBinding mViewBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mViewBinding = ActivityMainBinding.inflate(getLayoutInflater());
  setContentView(mViewBinding.getRoot());
  ...
}

使用的时候在Activity的onCreate方法里调用其静态inflate方法,返回ViewBinding实例,通过ViewBinding实例可以直接访问布局文件中带id的控件,比如上面的TextView,mViewBinding.tvTextView

如果想在生成绑定类时忽略某个布局文件,将tools:viewBindingIgnore="true"属性添加到相应布局文件的根视图中。

3.2 在Fragment中使用

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FragmentViewBindBinding binding = FragmentViewBindBinding.inflate(inflater, container, false);
    return binding.getRoot();
}

3.3 在Dialog中的使用

public class MyDialog extends Dialog {

    protected View mView;
    protected DialogBottomBinding mBinding;
    
    public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);

        //原来的写法
        mView = View.inflate(getContext(), getLayoutId(), null);

        //使用ViewBinding的写法
        mBinding = DialogBottomBinding.inflate(getLayoutInflater());
        mView = mBinding.getRoot();
        
        setContentView(mView);
    }
}

3.4 在 Adapter 中的使用

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  ItemViewBinding viewBinding = ItemViewBinding.inflate(LayoutInflater.from(mContext));
  return new MyViewHolder(viewBinding);
}

3.5 自定义View中使用

如果我们的自定义View中使用了layout布局,比如layout_my_view.xml,如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是自定义布局"
        android:textSize="50sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

会生成一个名为LayoutMyViewViewBinding.java文件,在自定义View通过如下方式绑定,

public class MyView extends View {
    public MyView (Context context) {
      this(context, null);  
    }
    public MyView (Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MyView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutMyViewViewBinding viewBinding = LayoutMyViewViewBinding.inflate(LayoutInflater.from(getContext()), this, true);
    }
}

如果自定义View布局文件中使用merge标签,

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是自定义merge"
        android:textSize="50sp" />

</merge>

此时要写成下面这个样子,

LayoutMyViewViewBindingviewBinding=LayoutMyViewViewBinding.inflate(LayoutInflater.from(context),this);

include 标签的使用

include 标签不带 merge 标签,需要给 include 标签添加 id, 直接使用 id 即可,用法如下所示。

<include
  android:id="@+id/include"
  layout="@layout/layout_include_item" />

ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater);
binding.include.includeTvTitle.setText("使用 include 布局中的控件, 不包含 merge");

include 标签带 merge 标签,需要通过bind()将merge布局绑定到主布局上,用法如下所示。

<include
  layout="@layout/layout_merge_item" />

ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater);
LayoutMergeItemBinding mergeItemBinding = LayoutMergeItemBinding.bind(binding.getRoot());
mergeItemBinding.mergeTvTitle.setText("使用 include 布局中的控件, 包含 merge");

4.原理

原理就是Google在那个用来编译的gradle插件中增加了新功能,当某个module开启ViewBinding功能后,编译的时候就去扫描此模块下的layout文件,生成对应的binding类。

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

    @NonNull
    public final TextView tvTextView;

    private ActivityMainBinding(@NonNull ConstraintLayout rootView,
                                            @NonNull TextView rvDataList) {
        this.rootView = rootView;
        this.tvTextView = tvTextView;
    }

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

可以看出,最终使用的仍然是findViewById,和ButterKnife异曲同工,不同的是ButterKnife通过编译时注解生成ViewBinding类,而ViewBinding是通过编译时扫描layout文件生成ViewBinding类。

5. 优点

5.1 更早发现程序中部分错误:

发现程序错误的阶段不同:findViewById发生在运行时,ViewBinding作用在编译期。可以更早期的语法和发现错误。

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

Null 安全:由于视图绑定会创建对视图的直接引用,因此不存在因视图 ID 无效而引发 Null 指针异常的风险。此外,如果视图仅出现在布局的某些配置中(比如横竖屏布局内容差异),则绑定类中包含其引用的字段会使用 @Nullable 标记。

类型安全:每个绑定类中的字段均具有与它们在 XML 文件中引用的视图相匹配的类型。这意味着不存在发生类转换异常的风险。

这些差异意味着布局和代码之间的不兼容将会导致构建在编译时(而非运行时)失败。


大量预防性代码,繁琐冗余

5.2 使用更加方便简洁,相同的功能,使用的编码量更少

使用时findViewById需要提前声明视图,声明过程冗余繁琐。ViewBinding直接使用,简洁明了,易于维护,更少的代码实现相同的功能。


某类ViewBinding重构前后代码量对比

6.缺点

1、总代码量并没有减少,机械化的代码被自动工具生成,我们可以直接使用
2、通过布局文件查找使用的地方变得相对困难,需要手动输入生成后的类名查找
3、如果为了简化binding对象的获取需要用到反射实现,影响部分性能(对现在市面上的手机这点性能应该可以忽略不计)
4、如果为了取代ButterKnife,只能取代部分功能,针对ButterKnife点击之类的,需要转成原始的setOnClickListner()来实现。用 lambda表达式的话,影响不大。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容