kotlin 绑定控件_使用Kotlin使android view绑定很棒

kotlin 绑定控件

View Binding is a new feature in Android Studio 3.6, but this isn’t true at all. View Binding is a lighter variant of Android Data Binding. Why do we need View Binding? The answer is performance. Many developers useAndroid Data Binding only to have reference to views from Layout XML and ignore other features of the library. Code generation in View Binding is faster. But the default way of its usage isn’t so good.

视频 绑定是Android Studio 3.6中的一项新功能,但事实并非如此。 View Binding是Android Data Binding的一种较轻的变体。 为什么我们需要视图绑定? 答案是性能。 许多开发人员仅使用Android Data Binding来引用Layout XML中的视图,而忽略该库的其他功能。 视图绑定中的代码生成速度更快。 但是使用它的默认方式不是很好。

视图绑定:默认方式 (View Binding: default way)

Let’s look at the usage of View Binding in Fragment. We have a layout resource profile.xml (content isn’t important). Generated View Binding for the layout has name ProfileBinding. The default use of the binding will be the next:

让我们看看Fragment中View Binding的用法。 我们有一个布局资源profile.xml (内容并不重要)。 布局的生成的视图绑定的名称为ProfileBinding 。 绑定的默认用法将是下一个:

I disliked the next

我不喜欢下一个

  • Code with creating and destroying viewBinding

    创建和销毁viewBinding代码

  • Copy-paste: every Fragment has the same code

    复制粘贴:每个Fragment都有相同的代码
  • viewBinding property is nullable and mutable. Isn’t good

    viewBinding属性是可为空且可变的。 不好

Let’s use Kotlin Power and fix all of that

让我们使用Kotlin Power修复所有问题

Kotlin委托财产 (Kotlin Delegated Property)

Using Kotlin Delegated Properties we can reuse part of code and simplify tasks. I used it to simplify the usage of ViewBinding. I have made a delegate that wraps the creation and destroying of aViewBinding :

使用Kotlin委托的属性,我们可以重用部分代码并简化任务。 我用它来简化ViewBinding的用法。 我做了一个包装ViewBinding创建和销毁的ViewBinding

Using the delegate I’ve refactored ProfileFragment :

使用委托,我重构了ProfileFragment

The task has been solved. What could go wrong?

任务已解决。 可能出什么问题了?

出了些问题 … (Something went wrong …)

After the refactoring, I need to clean views inside viewBinding:

重构之后,我需要清理viewBinding内部的viewBinding

But as a result, I’ve gotten state that reference to ViewBinding inside the delegated property is null. The reason is how ViewLifecycleOwnerof a Fragment notifies about ON_DESTROY lifecycle event. The event occurs before Fragment.onDestroyView(), that’s why I need to clear viewBinding only after all operations on the main thread will complete. It can be done using Handler.post. The fixed delegate is

但是,结果是,我得到的状态是对委托属性内对ViewBinding的引用为null。 原因是如何ViewLifecycleOwner一个的Fragment约通知ON_DESTROY生命周期事件。 该事件发生在Fragment.onDestroyView()之前,这就是为什么仅在主线程上的所有操作完成后才需要清除viewBinding的原因。 可以使用Handler.post完成。 固定代表是

Android View Binding is a very good solution to remove findViewById(). The library killed Butter Knife. A combination of it with the power of Kotlin Delegated Property will make your code cleaner and readable. All source code can be found here and you can use the solution in your project and adopt it.

Android View Binding是删除findViewById()很好的解决方案。 图书馆杀死了黄油刀 。 将其与Kotlin委托财产的功能相结合,将使您的代码更清晰易读。 所有源代码都可以在这里找到,您可以在项目中使用该解决方案并采用它。

You can find the soultion as library here:

您可以在此处找到备忘库:

Do you want to know more about Android development? Subscribe to Android Broadcast Telegram Channel (Russian) or English version.

您是否想进一步了解Android开发? 订阅Android广播电报频道 (俄语)或英语版本

翻译自: https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719

kotlin 绑定控件