Preliminary study on View Binding in Android Studio3.6 and its usage difference

  • 2021-11-24 02:51:27
  • OfStack

Reference translation: https://developer.android.google.cn/topic/libraries/view-binding

View Binding is a feature that makes it easier to write code that interacts with views. When view binding is enabled in a module, it generates 1 binding class for each XML layout file that exists in the module. An instance of the bound class contains a direct reference to all views that have ID in the corresponding layout.
In most cases, the view binding replaces findViewById.

Setting instructions

View binding is available in Android Studio 3.6 Canary 11 +.
To enable view binding in the module, add the viewBinding element to the build. gradle file, as shown in the following example:


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

If you want to ignore the layout file when building the binding class, add the tools: viewBindingIgnore= "true" attribute to the root view of the layout file


<LinearLayout
  ...
  tools:viewBindingIgnore="true" >
 ...
</LinearLayout>

Usage

If view binding is enabled for a module, 1 binding class is generated for each XML layout file it contains. Each binding class contains references to the root view and all views with ID. The name of the binding class is generated by converting the name of the XML file to a hump case and adding the word "Binding" to the end.
For example, given a single named result_profile. xml:


<LinearLayout ... >
 <TextView android:id="@+id/name" />
 <ImageView android:cropToPadding="true" />
 <Button android:id="@+id/button"
  android:background="@drawable/rounded_button" />
</LinearLayout>

The resulting binding class will be called ResultProfileBinding. This class has two fields: an TextView named name and an Button named button. ImageView in the layout does not have ID, so there is no reference to it in the bound class.

Each binding class also includes an getRoot () method that provides a direct reference to the root view of the corresponding layout file. In this example, the getRoot () method in the ResultProfileBinding class returns the LinearLayout root view.


private ResultProfileBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 binding = ResultProfileBinding.inflate(layoutInflater);
 setContentView(binding.root);
}

An instance of a bound class can now be used to reference any view:


binding.name.text = viewModel.name;
binding.button.setOnClickListener(new View.OnClickListener() {
 viewModel.userClicked()
});

Difference from findViewById

View binding has important advantages over using findViewById:
Null security: Because the view binding creates a direct reference to the view, there is no risk of null pointer exceptions due to an invalid view ID. In addition, when the view exists only in some configurations of the layout, the fields that contain its references in the bound class will be used with @ Nullable
• Type safety: Fields in each bound class have a type that matches the view they reference in the XML file. This means that there is no risk of class cast exceptions.

These differences mean that incompatibility between layout and code will cause compilation to fail at compile time rather than run time.

Difference between data and binding Library

Both view binding and data binding libraries generate binding classes that can be used to directly reference views. However, there are obvious differences:
The data binding library only handles data-bound layouts created using tags.
• view binding does not support layout variables or layout expressions and therefore cannot be used to bind layouts to XML data.


Related articles: