Original addresshttps://blog.csdn.net/fly_with_24/article/details/104337067
The following is the author’s reading record

Google’s role in 2019 I / O ConferenceWhat’s New in Architecture ComponentsThis paper introduces view binding
InWhat’s New in Architecture ComponentsIn, there is a short speech on view binding, which compares view binding with existing solutions and further discusses why view binding is better than existing solutions such as , data binding , or , kotlin synthetics , etc.
To me, kotlin synthetics works well, but there is no compile time security, which means that all IDs are in the global namespace. Therefore, if you use an ID with the same name and import the ID from the wrong layout, it will crash because the ID is not part of the current layout, which you cannot know in advance unless you run the application to the layout.
This article provides a good overview of kotlin synthetics
The Argument Over Kotlin Synthetics
View binding will be available in Android studio 3.6 stable version. If you want to use it, you can download Android studio 3.6 RC3 or Android Studio 4.0 Canary 9
The main advantage of view binding is that all binding classes are generated by the gradle plug-in, so it has no impact on build time and has compile time security (we will see in the example).
First of all, to enable view binding, we need to set the view binding in the build.xml of module Add the following to the gradle file:

Note: View binding is enabled on a module by module basis, so if you have a multi module project setup, you need to set it in each build Add the above code to the gradle file.
If you want to disable view binding in a specific layout, you need to add {tools: viewbindingignore = “true” in the root view of the layout file.
Once enabled, we can start using it immediately, and when you finish synchronizing build Gradle file, all binding classes are generated by default.
It generates the binding class by converting the XML layout file name to hump case and adding , binding , at the end. For example, if your layout file is named activity_ Splash, it will generate a binding class of {activitysplashbinding.
How to use it?
Used in activity

We have an activity called_ In the layout file of splash , there is a , textview with ID , tvversionname , so when using view binding, all we have to do is get the reference of the binding class, for example:

Use getroot() in the setcontentview() method, which will return the root layout of the layout. The view can be accessed from the binding class object we created, and it can be used immediately after the object is created, as shown below:

Here, the binding class knows that tvversionname is textview, so we don’t have to worry about type conversion.
Used in fragment

In the fragment, the use of view binding is somewhat different. We need to pass a boolean variable of layoutinflator, ViewGroup and attachtoroot which are obtained by overriding oncreateview .
We can call} binding Root} returns view. You also notice that we use two different variables , binding , and_ Binding, and_ The binding # variable is set to null in # ondestroyview().
This is because the lifecycle of the fragment is different from that of the activity, and the fragment can exceed the lifecycle of its view, so if it is not set to null, a memory leak may occur.
Another variable passed!! Making one variable nullable and another non nullable avoids null checking.
Use in recyclerview adapter

row_ payment. XML , is the layout file we use for , recyclerview , item, which corresponds to the generated binding class , rowpaymentbinding.
Now, all we need to do is call the inflate () method in oncreateviewholder () to generate the rowpaymentbinding object, pass it to the paymentholder main constructor, and Root , passed to , recyclerview Viewholder() constructor.
Process < include > tags
View binding can be used with the < include > tag. Layouts usually contain two < include > labels, with or without < merge > labels.
< include > without < merge > tag
We need to assign an ID to < include >, and then use that ID to access the views in the included layout. Let’s look at an example.
app_bar.xml

main_layout.xml

In the above code, we include a general toolbar in the layout file, < include > has an “Android: id =” @ + ID / AppBar “ID, which we will use from the” app “_ bar. Access the toolbar in XML and set it as our action bar.

< include > with < merge > tag
When we include another layout in one layout, we usually use a layout marked with < merge >, which helps to eliminate layout nesting.
placeholder.xml

fragment_order.xml

If we try to provide an ID for this < include >, view binding will not generate an ID in the binding class, so we cannot access the view as we would with normal , include ,.
In this case, we have # placeholder binding, which is # placeholder XML (< merge > layout file). We can call its bind () method and pass the root view of the layout containing it.

Then, we can access # placeholder. From our class (such as # placeholder binding. Tvplaceholder. Text) XML} internal view.
Thanks for reading. We look forward to receiving your comments.
Android Developer docs — view binding
The above contents are reproduced in https://blog.csdn.net/fly_with_24/article/details/104337067
For the author’s reading only