It is time to embrace the ViewBinding! !

Shen Zhou thousands of junks sail past the
million trees ahead of the spring wood.
- Tang Liu Yuxi

I. Introduction

With the official release of Android Studio 3.6, I am duty-bound at the forefront update early adopters. AS upgrade smoothly as usual, after the reboot into the Gradle upgrade link is agonistic, you need to upgrade from 3.5.1 to 3.6.0. Sure enough, a problem! !

ButterKnife actually an error, the log is as follows:

D:\xxx\libbase\component\dialog\BottomDialog.java:33:     : Attempt to use @BindView for an already bound ID 0 on 'mTvNegative'. (com.xxx.libbase.component.dialog.BottomDialog.mLayoutContent)
    ViewGroup mLayoutContent;

I am really puzzled ah. Solve it, upgrade ButterKnife, turn over information to find issue, look at the source code, and so on and so forth. God eventually pays off, I will Gradle version rollback, and all return to calm. [If there is a solution, please let me know, grateful]

Second, the acquaintance ViewBinding

It ButterKnife omitted are the same as for the findViewById () such duplicate code. In fact, in 2019 Google developer summit ViewBinding has been heard, and after updating the control ID layout can immediately refer to the Activity, which is absolutely necessary to compile than ButterKnife, need to distinguish between R and R2 to be more comfortable.
Upgrade to 3.6.0 above is to use it, but the reality is always so cruel, nine out of ten unsatisfactory, ViewBinding and ButterKnife seems only the second election.

Third, embrace ViewBinding

About ViewBinding documents, official written in great detail, see the view of the binding . This paper things simple, say some of the main issues under the official Google not mentioned.

3.1 Environmental Requirements

  • Android Studio version 3.6 and above
  • Gradle plug-in version 3.6.0 and above

3.2, open ViewBinding function

ViewBinding support is enabled by module, add the following code build.gradle file module:

android {
        ...
        viewBinding {
            enabled = true
        }
}    

3.3, Activity in use ViewBinding

//之前设置视图的方法
setContentView(R.layout.activity_main);

//使用ViewBinding后的方法
mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
setContentView(mBinding.getRoot());

You can see, when you use the ViewBinding, activity_main.xml for your file, it will automatically help you generate a ActivityMainBinding.java file (the file in the build / generated / data_binding_base_class_source_out / xxx ... catalog), which is the layout file Binding hump nomenclature plus a suffix, then it is used directly in an Activity.

3.3.1, the layout of the controls directly

When we add an id in the layout tv_text after TextView directly used in the Activity mBinding.tvText to get the control. As shown below, also seen in a control hump nomenclature to the acquired ID:

mBinding.tvText.setText("是你得不到的ViewBinding");

3.3.2, the layout of import controls

For example, we have layout_comment.xml layout, the layout has the id tv_include of the TextView, code is as follows:

<?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:id="@+id/tv_include"
        android:text="这就是测试啊"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Then activity_main.xml the include the layout file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

</androidx.constraintlayout.widget.ConstraintLayout>

So how do we use this time to layout_comment.xml layout TextView control it, first of all include labels need to declare id, for example layout_include, then Activity code as follows:

mBinding.layoutInclude.tvInclude.setText("这就是你的不对了");

Is not it amazing, is not very simple.

Note:
When you add id to the root of the layout layout_comment.xml (such as adding a layout_xxx of ID) when being given at this time:

java.lang.NullPointerException: Missing required view with ID: layout_xxx
发布了40 篇原创文章 · 获赞 47 · 访问量 7万+

Guess you like

Origin blog.csdn.net/u010976213/article/details/104501830