简体   繁体   English

空 Android 数据绑定

[英]Null Android data-binding

While studying android data-binding, my colleague told me that the android data-binding can be null in few cases, also when one layout includes another layout with data-binding, the generated data-binding file annotate the binding of another layout as @Nullable.在研究android数据绑定时,我的同事告诉我,android数据绑定在少数情况下可以为空,而且当一个布局包含另一个具有数据绑定的布局时,生成的数据绑定文件将另一个布局的绑定注释为@可以为空。 My question is can data-binding be null and if yes when?我的问题是数据绑定可以为空吗?如果是,什么时候?

Data binding is a blueprint.数据绑定是一个蓝图。 A class that is created at compile time when it sees the "layout" tag.在编译时看到“layout”标签时创建的类。 The blueprint class will be named LayoutNameBinding Pascal Case.蓝图类将命名为 LayoutNameBinding Pascal Case。

Just like any other class, it is non-existent until you reserve memory for it and new it up.就像任何其他类一样,它是不存在的,除非您为它保留内存并对其进行更新。

So when you use the data binding utility on the onCreate it creates the class and you can store that in a local variable for using later.因此,当您在 onCreate 上使用数据绑定实用程序时,它会创建该类,您可以将其存储在局部变量中以供以后使用。

example:例子:

   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    binding.activity = this
    binding.iBindingRecyclerView = this
    binding.navHeader?.activity = this

    setupNavigationDrawer()
}

"layout root xml files that have other layout root xml files nested in them or named include layouts" will be added as classes inside the parent NameOfLayoutBinding class. “具有嵌套在其中的其他布局根 xml 文件或命名为包含布局的布局根 xml 文件”将作为类添加到父 NameOfLayoutBinding 类中。 These will not be null as they are auto generated at compile time so when you new up the parent, the children will exist.这些不会为空,因为它们是在编译时自动生成的,因此当您新建父级时,子级将存在。

So a databinding will not be null if you are referring to the auto generated class and if you are newing it up in your onCreate method appropriately.因此,如果您指的是自动生成的类,并且如果您在 onCreate 方法中适当地更新它,则数据绑定不会为空。

Now a failed binding event due to a null object can happen if you didn't pass in the variable that you are binding to, but that is not the question you asked.现在,如果您没有传入要绑定到的变量,则可能会发生由于空对象导致的绑定失败事件​​,但这不是您问的问题。

Hope that helps, if you meant something different, please digress.希望有所帮助,如果您的意思不同,请离题。

Finally, after so many years found an explaination for this.终于,经过这么多年,找到了对此的解释。 In case you are manually executing the DataBinding.inflate function then the returned DataBinding won't be null.如果您手动执行 DataBinding.inflate 函数,则返回的 DataBinding 不会为空。

For auto generated bindings (ie when you include a layout and binding is generated and assigned to a variable in the parent layout binding), the data binding can be null based on some situation.对于自动生成的绑定(即,当您包含一个布局并生成绑定并将其分配给父布局绑定中的变量时),根据某些情况,数据绑定可以为空。

Eg: Let's say we have layout as follow:例如:假设我们的布局如下:

Portrait Mode layout: /src/res/layout/activity.xml纵向模式布局:/src/res/layout/activity.xml

<LinearLayout ...>
    <include
      android:id="@+id/main_content"
      layout="@layout/main_content_layout"
    />

</LinearLayout>

And for Lanscape mode: /src/res/layout-land/activity.xml对于 Lanscape 模式:/src/res/layout-land/activity.xml

<LinearLayout ...>
    <include
      android:id="@+id/sidebar"
      layout="@layout/sidebar_layout"
    />

    <include
      android:id="@+id/main_content"
      layout="@layout/main_content_layout"
    />

</LinearLayout>

Now here as the multiple layout files are for same purpose but with different configuration (lanscape mode and portrait mode) the Android DataBinding will generate ActivityBinding.java file.现在这里因为多个布局文件的目的相同但具有不同的配置(横向模式和纵向模式),Android DataBinding 将生成 ActivityBinding.java 文件。 Now here the developer would need to access binding for both sidebar and main content using object of ActivityBinding.java class.现在,开发人员需要使用 ActivityBinding.java 类的对象访问侧边栏和主要内容的绑定。 As the sidebar is not present in portrait mode layout file, the binding file won't have any reference.由于侧边栏不存在于纵向模式布局文件中,因此绑定文件将没有任何参考。 Hence the reference for sidebar binding would be kept as Nullable.因此,侧边栏绑定的引用将保持为 Nullable。

Hence, for the layout files with same name for different configuration and with different view hierarchy, the inner binding object generated can have null value, due to which the data binding may have Nullable binding fields.因此,对于不同配置和不同视图层次结构的同名布局文件,生成的内部绑定对象可以具有空值,因此数据绑定可能具有可空绑定字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM