On the View Binding

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

View Binding is a feature that allows you to more easily write code to interact with the view. After enabling views bindings in the module, it will generate a binding class for each XML layout file module exists. Examples of binding class contains a direct reference to all views in the corresponding ID having layout.
In most cases, the view bindings replace findViewById.

Setting Description

In view of the binding Android Studio 3.6 Canary 11+ available.
In view of the need to enable binding module, set file build.gradle viewBinding element is added, in the following example:

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

If the layout file to be ignored when generating the binding classes, then the tools: viewBindingIgnore = "true" attribute to the root view of the layout file

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

Instructions

If you enable the view of the binding for the module, then for each XML layout file is generated that contains a binding class. Each binding class contains a reference to the root view and all views have ID's. Name Binding class by converting the XML file name for the camel case and the word "Binding" added to the end to production.
For example, given a 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>

Generated binding class will be referred ResultProfileBinding. The class has two fields: a name name TextView and a Button called the button. Layout ImageView no ID, so there is no reference to it in the binding class.

Each class also includes a bind the getRoot () method, to provide reference directly to the root view corresponding layout file. In this example, getRoot ResultProfileBinding class () method returns LinearLayout root view.

private ResultProfileBinding binding;

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

Examples of binding class reference may now be used in any view:

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

The difference between findViewById

Compared with the use findViewById, view bindings have important advantages:

  • Air safety: Due to bind the view creates a direct reference to the view, and therefore will not lead to an invalid view ID null pointer exception risks. Field Further, when the view is only present in certain configurations of the layout, which contains the referenced class will be used in the binding @Nullable
  • Type safety: each binding class field has its type referenced in the XML file matches the view. This means there is no risk class cast exceptions.

These differences mean that incompatibilities between layout and code causes the compiler to compile time rather than fail at run time.

The difference between data binding library

Binding and view data can be used to generate libraries are bound reference binding direct view type. However, there are significant differences:

  • only use data binding process library Tag creates data binding layout.
  • view binding does not support variable layout or layout expression, and therefore can not be used with the layout XML data binding.

Guess you like

Origin www.cnblogs.com/eagle816/p/12021907.html
Recommended