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表达式的话,影响不大。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,682评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,277评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,083评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,763评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,785评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,624评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,358评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,261评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,722评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,030评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,737评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,360评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,941评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,057评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,237评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,976评论 2 355

推荐阅读更多精彩内容