数据库中视图绑定到架构上

(Introduction)

In the existing environment, whenever we create a new screen, we have to manually link views in the layout with Java/Kotlin files using findViewById(). It’s time-consuming, error-prone, and means we’re using more boilerplate code.

在现有环境中,无论何时创建新屏幕,都必须使用findViewById()手动将布局中的视图与Java / Kotlin文件链接。 这很耗时,容易出错,这意味着我们正在使用更多样板代码。

If we take a more in-depth look at the process, we can find a pattern that can be automated. For example, what we do is declare a view in the Java/Kotlin file and initialize that view with the appropriate view id from inflated layout (referencing every view with id in the layout to the Java/Kotlin file).

如果我们对流程进行更深入的了解,我们将找到可以自动化的模式。 例如,我们要做的是在Java / Kotlin文件中声明一个视图,并使用膨胀的布局中具有适当视图id视图进行初始化(将布局中id每个视图都引用到Java / Kotlin文件中)。

The idea behind ViewBinding from the Android team is to create a binding class for every layout that holds the references of the views in the layout. This reduces boilerplate code in your component (activity and fragment), and we can access views by creating an instance to that class, which you’re going to learn shortly.

ViewBinding背后的想法 来自Android团队的建议是为每个布局创建一个绑定类,以容纳布局中视图的引用。 这减少了组件中的样板代码(活动和片段),并且我们可以通过为该类创建实例来访问视图,您将很快学习。

View binding replaces findViewById.

视图绑定替换 findViewById 。

(Integration)

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

注意: Android Studio 3.6 Canary 11+中 提供了视图绑定 。

View binding can be enabled at the module level. So, you have multiple modules in your project, you have to enable it for every module. To enable view binding in a module, you need to add the following lines under the android tag in the module level build.gradle file:

可以在模块级别启用视图绑定。 因此,您的项目中有多个模块,必须为每个模块启用它。 要在模块中启用视图绑定,您需要在模块级别build.gradle文件的android标记下添加以下行:

viewBinding {
    enabled = true
}

(Usage)

After enabling view binding, whenever we create a new layout, a binding file with reference to the views in the layout is generated. The root of the layout is indicated with a view named root in the binding file. We can use root in setContentView to inflate layout in UI components.

启用视图绑定后,无论何时我们创建新的布局,都会生成一个引用该布局中的视图的绑定文件。 布局的root绑定文件中名为root的视图指示。 我们可以在setContentView使用root来增加UI组件中的布局。

Let’s create a layout file and inflate it with view binding. Take a look at the layout file:

让我们创建一个布局文件,并使用视图绑定对其进行充气。 看一下布局文件:

Now let’s create a binding instance of the layout:

现在让我们创建布局的绑定实例:

private lateinit var activityMainBinding: ActivityMainBinding

(View Binding in Activities)

To use an instance of the binding class in the activity, perform the following steps in the activity’s onCreate() method:

要在活动中使用绑定类的实例,请在活动的onCreate()方法中执行以下步骤:

  1. Call the static inflate() method in the generated binding class. This creates an instance of the binding class which we can use. 在生成的绑定类中调用静态inflate()方法。 这将创建我们可以使用的绑定类的实例。
  2. As mentioned, get a reference to the root view by either calling the getRoot() function or using Kotlin property syntax. 如前所述,可以通过调用getRoot()函数或使用Kotlin属性语法来获取对根视图的引用。
  3. Pass the root view to setContentView() method to render the layout on the screen. 将根视图传递给setContentView()方法以在屏幕上呈现布局。

Take a look:

看一看:

(View Binding in Fragments)

To use an instance of the binding class in the fragment, perform the following steps in the fragment’s onCreateView() method:

要在片段中使用绑定类的实例,请在片段的onCreateView()方法中执行以下步骤:

  1. Call the static inflate() method in the generated binding class. This creates an instance of the Binding class to use in the fragment. 在生成的绑定类中调用静态inflate()方法。 这将创建Binding类的实例以在片段中使用。
  2. Get a reference to the root view by either calling the getRoot() method or using Kotlin property syntax. 通过调用getRoot()方法或使用Kotlin属性语法来获取对根视图的引用。
  3. Return the root view from the onCreateView() method to render the layout on the screen. 从onCreateView()方法返回根视图,以在屏幕上呈现布局。

You can now use this instance to reference the views in the layout and perform your actions:

现在,您可以使用此实例来引用布局中的视图并执行操作:

binding.greetings.text = viewModel.msg

(Advantages)

(Null safety)

With view binding, you no longer need to do the null check on a view while performing actions on it. As the binding class generated directly from the layout file, it only contains the views within the layout.

使用视图绑定,您在对视图执行操作时不再需要对视图执行null检查。 作为直接从布局文件生成的绑定类,它仅包含布局中的视图。

(Type safety)

Type safety is one of the most significant advantages in view binding. If you try to assign text watcher to a button, it won’t accept because the binding class contains the reference with appropriate view types.

类型安全是视图绑定中最重要的优点之一。 如果您尝试将文本监视程序分配给按钮,则该按钮将不被接受,因为绑定类包含具有适当视图类型的引用。

(Ease of use)

View binding sole purpose is to replace the use of findviewbyId. Unlike data binding, it’s lightweight so that it won’t take much time for compilation. At the same time, it won’t support two-way binding like data-binding. With view binding, there is no need to mention layout-tag in every layout file as you do in data-binding.

视图绑定的唯一目的是代替使用findviewbyId 。 与数据绑定不同,它是轻量级的,因此不需要太多时间进行编译。 同时,它不会像数据绑定那样支持双向绑定。 使用视图绑定,不需要像在数据绑定中那样在每个布局文件中都提及layout-tag。

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/everything-you-should-know-about-viewbinding-in-android-52552af9e8ba

数据库中视图绑定到架构上