It's time to embrace ViewBinding!!
Thousands of sails pass by the side of the sunken boat,
In front of the sick tree, there are thousands of trees.
Liu Yuxi, Tang Dynasty
I. Preface
With the official release of Android Studio 3.6, I am at the forefront of the update. AS always, the upgrade of AS went smoothly. After the restart, it entered the bloody upgrade of Gradle, which needs to be upgraded from 3.5.1 to 3.6.0. Sure enough, something's wrong!!
ButterKnife reported 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 can't think. To solve this problem, upgrade ButterKnife, search for information, issue, source code, etc. In the end, it's all right. I've backed off the Gradle version, and everything is back to peace. [if there is a solution, please let me know, thank you very much]
2, Getting to know ViewBinding
It's the same as ButterKnife in order to eliminate duplicate code like findViewById(). In fact, we have heard about ViewBinding at the Google developers summit 2019. After the control ID is updated in the layout, it can be referenced in the Activity immediately. This is far more comfortable than ButterKnife's need to compile and distinguish between R and R2.
The above upgrade to 3.6.0 is to use it. However, the reality is always so cruel. Nine times out of ten, it's unsatisfactory. It seems that ViewBinding and ButterKnife can only choose one from the other.
3, Embrace ViewBinding
The official document about ViewBinding is very detailed, please see View binding . In this article, I will briefly talk about some problems that Google didn't mention officially.
3.1 environmental requirements
- Android Studio version 3.6 and above
- Gradle plug in version 3.6.0 and above
3.2. Open the ViewBinding function
ViewBinding supports enabling by module. Add the following code to the build.gradle file of the module:
android { ... viewBinding { enabled = true } }
3.3 use of ViewBinding in Activity
//How to set up the view before setContentView(R.layout.activity_main); //Method after using ViewBinding mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this)); setContentView(mBinding.getRoot());
As you can see, when you use ViewBinding, an Activity main Binding.java file will be automatically generated for you according to your Activity main.xml file (the file is in build / generated / data ﹣ Binding ﹣ base ﹣ class ﹣ source ﹣ out / xxx Directory), that is, the hump naming method of the layout file plus a Binding suffix, and then it can be used directly in the Activity.
3.3.1. Direct controls in layout
When we add a TextView with the ID of TV? Text in the layout, we can get the control directly by using mBinding.tvText in the Activity. As shown below, you can see that it is also obtained by hump naming method of control ID:
mBinding.tvText.setText("You can't get it ViewBinding");
3.3.2 imported controls in layout
For example, we have a layout of layout_comment.xml. In the layout, there is a TextView with the id of tv_include. The 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="This is the test" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Then include the layout in the activity main.xml 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>
Then how can we use the TextView control in the layout_comment.xml layout? First, the include tag needs to declare the id, such as layout_include, and then the code in the Activity is as follows:
mBinding.layoutInclude.tvInclude.setText("That's your fault");
Is it magic? Is it simple.
Be careful:
When you add an ID (such as the ID of layout XXX) to the root layout of layout_comment.xml, an error will be reported:
java.lang.NullPointerException: Missing required view with ID: layout_xxx