Android Studio 3.6 New Feature View Binding ViewBinding Usage Guide

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

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.

Text

ViewBinding function is introduced in Android Studio 3.6 Canary 11 and later, and ViewBinding will gradually replace findViewById. What are you waiting for? Take the time to learn!
Google's official document ViewBinding Demo is written in Kotlin language, which looks unfamiliar and takes a little time. I implemented similar code on Java, or look at Java!

The following is a brief introduction to how to use ViewBinding:

Modification of build. gradle

Add the viewBinding element to its build. gradle file, and then re-add sync


// Android Studio 3.6.0
android {
 ...
 viewBinding {
  enabled = true
 }
 }

In Android Studio 4.0, viewBinding will be incorporated into the buildFeatures option, and the configuration will be changed to:


// Android Studio 4.0
android {
 buildFeatures {
 viewBinding = true
 }
}

Modify java code

If your layout file is activity_main. xml, a class of ActivityMainBinding will be generated, if your layout file is result_profile. xml, a class of ResultProfileBinding will be generated, and so on.
The following is illustrated by activity_main. xml and its corresponding MainActivity. java:

Suppose activity_main. xml places three controls: TextView (Id is text), Button (Id is button), and ImageView (Id is not set), where ImageView cannot be referenced in code because there is no reference to it in the binding class because Id is not set.

Part of the code for MainActivity. java is as follows:


@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
 LayoutInflater layoutInflater = LayoutInflater.from(this);
 ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater);
 setContentView(binding.getRoot());

 binding.text.setText(" Text has changed ");
 binding.button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  Toast.makeText(getApplicationContext(), "Button Be clicked ", Toast.LENGTH_SHORT).show();
  }
 });
 }

Matters needing attention

Note: The original setContentView (R.layout.activity_main) needs to be commented out, otherwise ContentView will be set repeatedly.
The root view of the layout (activity_main. xml) automatically generates a member variable named rootView. In the onCreate () method of Activity, rootView is passed into the setContentView () method, so that Activity can use the layout in the bound object, and rootView is a private variable, which needs to be obtained by using the getRoot () method.

Engineering code

Download the complete project from Code Cloud or Github (ViewBindingTest directory):
https://gitee.com/lwjobs/AndroidStudy
https://github.com/lwjobs/AndroidStudy

For a detailed description of view binding ViewBinding, please refer to:

Use view binding instead of findViewById

Summarize


Related articles: