This article is first published in the wechat public account “Android development journey”, welcome to follow, get more technical dry goods
Wan-android Jetpack VersionAndroid Jetpack architecture for componentized application developmentWelcome to star
Project address of Flutter wan-AndroidFlutter Wan – AndroidWelcome to star
preface
During Android development, we always needed to get the ViewId from the XML layout in order to assign values to it for display. In the early days, we had to use the findViewById API, which resulted in a lot of template code. Around 2013, Android developer Jake Wharton opened source the Butter Knife framework to make it easy for developers to get viewid through Bind(“viewid”). We started using Android Kotlin Extensions in the last two years due to Google’s support for Kotlin. Import the layout file in the file and reference viewId directly. No additional operations required, most convenient.
Google has now added a new ViewBinding tool, ViewBinding, to Android Studio 3.6 Canary 11 and later.
Let’s take a look at the specific use.
Using ViewBinding
Many of our current projects use modularity. ViewBinding is also smart and can be enabled by module. To enable ViewBinding in a module, add the following configuration to the build.gradle module:
android {
...
viewBinding {
enabled = true}}Copy the code
If the developer does not want to generate a Binding class for a layout file during use, it can be added to the root view of the layout using the following properties:
<LinearLayout
...
tools:viewBindingIgnore="true">... </LinearLayout>Copy the code
After the view binding function is enabled in the module, the system generates a binding class for each XML file in the module. Each binding class contains references to the following view and all views with defined ids.
The name generation rule for the Binding class is to end the name of the XML file with a Binding following the camel name rule.
Take our activity_main.xml file.
<LinearLayout ... >
<TextView android:id="@+id/name" />
<ImageView android:cropToPadding="true" />
<Button android:id="@+id/button"
android:background="@drawable/rounded_button" />
</LinearLayout>
Copy the code
The name of the produced binding class is ActivityMainBinding. This class has two fields: a TextView named Name and a button named Button. The ImageView in this layout has no ID, so there is no reference to it in the binding class.
Each binding class also contains a getRoot() method that provides a direct reference to the root view of the layout file. In this example, the getRoot() method in the ActivityMainBinding class returns the LinearLayout root view.
The automatically generated binding class is also not complicated, consisting mainly of two inflate overloaded methods and a bind method. We get the viewId reference in the bind method, and we actually get the view internally through findViewById.
We usually set the layout file using setContentView(“layoutId”), but with ViewBinding we need to set the layout as follows:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) // Get name and assign binding.name.text ="viewBinding"}}Copy the code
So you can use it directly. Isn’t that easy?
However, it should be noted that if our layout file is divided into Layout and layout-land, we may have a different viewId when we define the layout. If we use findViewById or Butter Knife, we will definitely have an exception.
When we use ViewBinding, the binding class makes relevant judgments for us. Use @nullable and @nonNULL annotations to tell developers which views might be empty. And added a related gaze description to a view that might be empty.
/**
* This binding is not available in all configurations.
* <p>
* Present:
* <ul>
* <li>layout/</li>
* </ul>
*
* Absent:
* <ul>
* <li>layout-land/</li>
* </ul>
*/
@Nullable
public final TextView mAppTv;
Copy the code
Remind developers to pay attention to exception handling when using.
conclusion
Currently, ViewBinding features are not perfect, such as the inability to reference a view when an inClude tag is used in XML. But overall it’s pretty good. It’s much more convenient than findViewById and Butter Knife. ViewBinding is used without type conversions and null pointer exceptions. Because it’s all defined in the binding class. Developers can use it directly. Compared to the Android Kotlin Extensions, I think both are similar. It’s hard to say who’s better. In contrast to databinding, the databinding library only handles databinding layouts created using layout code. It has limitations.
Now Jake Wharton has added the following sentence to the Butter Knife open source library:
Attention: Development on this tool is winding down. Please consider switching to view binding in the coming months.
The role of ViewBinding in the future is clear.