[Android Jetpack series] 1. The use of ViewBinding

Notes about this series

As a series of articles for learning Jetpack, it may be updated very slowly. This series of articles should be called study notes...

Students who read this article should already have the ability to develop simple Android apps. If you have zero foundation, then reading this article may be a bit difficult to understand. I can only try to explain it simply.

The development environment and SDK version used in this article are as follows, and readers should use a development environment no lower than that used in this article.

Android Studio 4.0.1
minSdkVersion 21
targetSdkVersion 30

Regarding compileSdkVersionthe description of the compiled version, the higher the better, the higher the compiled version, the newer the SDK. Under such conditions, many new problems will generally appear, usually permission problems. So here it is 30(Android 10).

Regarding the code fragments in this article, the pseudocode form is adopted uniformly.

Regarding the programming language used in this article, it is used uniformly Kotlin.

About Jectpackthe introduction of , I won’t go into details here, Baidu…

text

Note: View binding is available in Android Studio 3.6 Canary 11 and higher.

viewBindingWhat is it? Directly translated into Chinese, the meaning is: 视图绑定.

Open viewBinding

First find and open the project , build.gradleand take two pictures.

build.gradle
build.gradle

How to open it? There are two opening methods introduced in the official document, let's look at them separately.

method one:

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

Method 2 (recommended):

android {
    
    
    ...
    buildFeatures {
    
    
        viewBinding true
    }
    ...
}

It is recommended to use here 方式二.

Some will still write like this, and it is also possible.

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

Use viewBinding

In Android development, the traditional search control is used findViewById, for example, res/layout/activity_main.xmlthe interface in is as follows:
activity_main.xml

...
<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>
...

The id of the corresponding TextView is myTextView, let's look at the traditional way of finding and using in the code myTextView.

...
override fun onCreate(savedInstanceState: Bundle?) {
    
    
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    //通过Id找到对应的view控件
    val myTextView: TextView = findViewById(R.id.myTextView)
    //设置新的文本内容
    myTextView.text = "你好 世界!"
    myTextView.setOnClickListener {
    
    
        it as TextView
        it.text = "你点击了我!"
    }
}
...

After using viewBinding, it should look like this.

...
//布局绑定类, 延迟加载
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
    
    
    super.onCreate(savedInstanceState)
    //binding初始化
    binding = ActivityMainBinding.inflate(layoutInflater)
    //将根视图作为内容视图
    setContentView(binding.root)
    //设置新的文本内容
    binding.myTextView.text = "你好 世界!"
    binding.myTextView.setOnClickListener {
    
    
        it as TextView
        it.text = "你点击了我!"
    }
}
...

At first glance, it seems that the amount of code has increased, and there is no advantage.

What if, so?

...
lateinit var myTextView: TextView
lateinit var myTextView2: TextView
lateinit var myTextView3: TextView
lateinit var myTextView4: TextView
lateinit var myTextView5: TextView
override fun onCreate(savedInstanceState: Bundle?) {
    
    
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    myTextView = findViewById(R.id.myTextView)
    myTextView2 = findViewById(R.id.myTextView2)
    myTextView3 = findViewById(R.id.myTextView3)
    myTextView4 = findViewById(R.id.myTextView4)
    myTextView5 = findViewById(R.id.myTextView5)

    myTextView.text = "你好 世界!"
    myTextView2.text = "你好 世界2!"
    myTextView3.text = "你好 世界3!"
    myTextView4.text = "你好 世界4!"
    myTextView5.text = "你好 世界5!"
}
...

After using viewBinding

...
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
    
    
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    binding.myTextView.text = "你好 世界!"
    binding.myTextView2.text = "你好 世界2!"
    binding.myTextView3.text = "你好 世界3!"
    binding.myTextView4.text = "你好 世界4!"
    binding.myTextView5.text = "你好 世界5!"
}
...

It is worth noting that once enabled, viewBindingall xmllayout files (layout) will generate a Bindingclass, if you need to filter out some xmlie: 部分xml不生成Binding类.

Then add the attribute to the root nodexml of .tools:viewBindingIgnore="true"

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

findViewByIddifference with

View binding has some significant advantages over using findViewById:

  • Null safety: Since view binding creates a direct reference to the view, there is no risk of a Null pointer exception being thrown due to an invalid view ID. Also, if the view only appears in certain configurations of the layout, the field containing its reference in the binding class is marked with @Nullable.
  • Type Safety: Fields in each bound class have types that match the views they reference in the XML file. This means that there is no risk of class conversion exceptions.

These differences mean that incompatibilities between layout and code will cause builds to fail at compile time rather than runtime.

viewBindingInsufficient use of

  • View binding does not support layout variables or layout expressions, so it cannot be used to declare dynamic interface content directly in an XML layout file.
  • View binding does not support two-way data binding.

In the development process, you need to decide whether to use it according to the actual situation viewBinding.

Guess you like

Origin blog.csdn.net/AoXue2017/article/details/126055557