flowable 绑定视图_深入探索视图绑定

flowable 绑定视图

Google introduced view binding in the What’s new in Android session at Google I/O 2019.

Google在Google I / O 2019的Android新功能会话中引入了视图绑定。

There is a brief talk about view binding in What’s New in Architecture Components, which compares view binding with existing solutions and further talks about why view binding is better than existing solutions like data binding or Kotlin synthetics.

在“架构组件的新增功能”中简要讨论了视图绑定,将视图绑定与现有解决方案进行了比较,并进一步讨论了为什么视图绑定比数据绑定或Kotlin合成等现有解决方案更好。

For me, Kotlin synthetics was working fine but all the IDs reside in a global namespace so if you are using an ID with the same name and if you import the ID from the wrong layout, you’re going to get a crash because the ID is not part of the current layout and there is no way to know this in advance unless you run your app and go to that layout.

对我而言,Kotlin合成材料工作正常,但所有ID都位于全局命名空间中,因此,如果您使用的是具有相同名称的ID,并且从错误的布局导入ID,则会因ID崩溃而崩溃不是当前布局的一部分,除非您运行应用程序并转到该布局,否则无法提前知道这一点。

The following article has a nice overview of problems with Kotlin synthetics.

以下文章很好地概述了Kotlin合成物的问题。

View binding will be available in Android studio’s 3.6 stable version but if you want to try it, you need to download Android Studio 3.6 RC 3.Edit: View Binding is now available for everyone as part of the Android Studio 3.6 stable release.

View绑定将在Android Studio 3.6稳定版中提供,但是如果您想尝试,则需要下载Android Studio 3.6 RC3。编辑:View Binding作为Android Studio 3.6稳定版的一部分现已面向所有人开放。

The main advantage of view binding is that all the binding classes are generated by the Gradle plugin so it doesn’t have an effect on build time and it has compile-time safety (which we will see in our examples).

视图绑定的主要优点是所有绑定类都是由Gradle插件生成的,因此它对构建时间没有影响,并且具有编译时安全性(我们将在示例中看到)。

Let’s start by enabling view binding. We need to add the following in our module’s build.gradle file:

让我们从启用视图绑定开始。 我们需要在模块的build.gradle文件中添加以下内容:

// Android Studio 3.6.X
android {
viewBinding {
enabled = true
}
}// Android Studio 4.0
android {
buildFeatures {
viewBinding = true
}
}

Note: View binding is enabled on a module-by-module basis so you need to add the above code in every build.gradle file if you have a multi-module project setup.

注意:视图绑定是逐模块启用的,因此,如果您具有多模块项目设置,则需要在每个build.gradle文件中添加以上代码。

If you want to disable the view binding specific layout then you need to add tools:viewBindingIgnore=”true” to the root view of the layout file.

如果要禁用视图绑定特定的布局,则需要在布局文件的根视图中添加tools:viewBindingIgnore=”true”

After enabling, we can start using it right away and all the binding classes are generated by default when you finish syncing your build.gradle file.

启用后,我们可以立即开始使用它,并且当您完成同步build.gradle文件时,默认情况下会生成所有绑定类。

It generates a binding class by converting the XML layout file name to the camel case and adding Binding at the end of it. For example, if your layout file is named activity_splash then it will generate the binding class as ActivitySplashBinding.

它通过将XML布局文件名转换为驼峰大小写并在其末尾添加Binding来生成绑定类。 例如,如果您的布局文件名为activity_splash 然后它将生成绑定类为ActivitySplashBinding

如何使用? (How Do I Use It?)

活动中 (In activity)

We have our layout file named activity_splash and in it, we have a TextView with ID tvVersionName so when using view binding, all we have to do it get the reference of the binding class, like:

我们有一个名为activity_splash的布局文件 在其中,我们有一个ID为tvVersionNameTextView 因此,在使用视图绑定时,我们要做的就是获取绑定类的引用,例如:

val binding: ActivitySplashBinding = ActivitySplashBinding.inflate(layoutInflater) 

And use its getRoot() in the setContentView() method which returns the root layout of our layout. Views will be accessible from the binding class object we created and we can use it right after creating the object like this:

并在setContentView()方法中使用其getRoot() ,它返回布局的根布局。 可以从我们创建的绑定类对象访问视图,并且可以在创建对象后立即使用它,如下所示:

binding.tvVersionName.text = getString(R.string.version)

Here, the binding class knows that tvVersionName is a TextView so we don't have to worry about typecasting.

在这里,绑定类知道tvVersionNameTextView因此我们不必担心类型转换。

片段中 (In fragment)

Using view binding is a little different in fragments. We need to pass LayoutInflator, ViewGroup, and an attachToRoot boolean variable which we get by overriding onCreateView().

在片段中,使用视图绑定有些不同。 我们需要传递LayoutInflatorViewGroup和一个attachToRoot布尔变量,这些变量是通过覆盖onCreateView()

We also need to return the view which we can do by returning the root view by calling binding.root. You also noticed that we are using two different variables and the _binding variable is set to null in onDestroyView().

我们还需要返回视图,这可以通过调用binding.root返回根视图来完成。 您还注意到,我们使用了两个不同的变量,并且_binding变量在onDestroyView()设置为null。

That’s because the fragment’s lifecycle is different from the activity’s and the fragment can outlive their views so we can get memory leaks if we don’t set it to null.

这是因为该片段的生命周期与活动的生命周期不同,并且该片段可以超出其视图的寿命,因此如果不将其设置为null,则可能会发生内存泄漏。

The other variable is there to avoid a null check with !! by making one variable nullable and the other one non-null.

另一个变量是为了避免用!!进行空检查!! 通过使一个变量为可空值而使另一个变量为非空值。

在RecyclerView适配器中 (In RecyclerView adapter)

row_payment.xml is our item layout file for the RecyclerView’s row and that’s why the RowPaymentBinding class gets generated.

row_payment.xml是我们针对RecyclerView的行的项目布局文件,这就是RowPaymentBinding的原因 类被生成。

Now, all we need to do is call the inflate() method and pass the required parameters that are available inside onCreateViewHolder() and pass the generated binding class object in the primary constructor of our PaymentHolder class and pass the itemBinding.root to the RecyclerView.ViewHolder() constructor. That’s it.

现在,我们要做的就是调用inflate()方法并传递onCreateViewHolder()内部可用的必需参数,并将生成的绑定类对象传递给PaymentHolder的主构造PaymentHolder 类,并将itemBinding.root传递给RecyclerView.ViewHolder()构造函数。 而已。

处理<include> d布局 (Dealing With the <include>d Layout)

View binding can be used with the <include> layout tag. There are usually two kinds of <include> tags included in layouts, with or without <merge>.

视图绑定可以与<include>布局标记一起使用。 布局中通常<include>两种<include>标签,带或不带<merge>

  • <include> without <merge>.

    <include>不带<merge>

We need to assign <include> an ID and then use that ID to access the view inside the included layout. Let’s see an example of that.

我们需要为<include>分配一个ID,然后使用该ID来访问包含布局中的视图。 让我们来看一个例子。

In the above code, we included a common toolbar in our layout file and <include> has an android:id=“@+id/appbar” ID which we will use to access the toolbar from app_bar.xml and set it as our support action bar.

在上面的代码中,我们在布局文件中包括了一个通用工具栏, <include>有一个android:id=“@+id/appbar” ID,我们将使用它从app_bar.xml访问工具栏并将其设置为我们的支持动作栏。

  • <include> with <merge>.

    <include><merge>

Sometimes we have a layout with the <merge> tag which helps to eliminate redundant view groups in your view hierarchy when including one layout within another.

有时,我们有一个带有<merge>标记的布局,当在一个布局中包含另一个布局时,有助于消除视图层次结构中的多余视图组。

And this layout is included as usual inside our layout file.

并且此布局像往常一样包含在我们的布局文件中。

If we try to give this <include> an ID, the view binding won’t generate the ID in the binding class so we can’t access the view as we did in the case of the normal include.

如果我们尝试给此<include>一个ID,则视图绑定将不会在绑定类中生成ID,因此我们无法像使用普通include那样访问视图。

In this case, we have PlaceholderBinding which is an auto-generated class for placeholder.xml (our <merge> layout file). We have to call its bind() method and pass the root view of the layout in which we included it.

在这种情况下,我们有PlaceholderBinding 这是placeholder.xml (我们的<merge>布局文件)的自动生成的类。 我们必须调用其bind()方法并传递包含它的布局的根视图。

Then, we can access views inside our placeholder.xml from our class like placeholderBinding.tvPlaceholder.text.

然后,我们可以从我们的类(如placeholderBinding.tvPlaceholder.text访问placeholder.xml内部的视图。

Thanks for reading. I’d love to hear from you in the comments.

谢谢阅读。 希望收到您的评论。

翻译自: https://medium.com/better-programming/exploring-viewbinding-in-depth-598925821e41

flowable 绑定视图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值