简体   繁体   English

Android数据绑定:示例与现实

[英]Android Data-Binding : Example vs Reality

In all the data-binding examples that shows Generic data type handling developer.android.com uses real char < and > . 在所有显示Generic数据类型处理的数据绑定示例中, developer.android.com使用real char <>

在此处输入图片说明

but when it comes to reality 但是当涉及到现实时

在此处输入图片说明

I am getting below error. 我正在错误以下。

The value of attribute "type" associated with an element type "variable" must not contain the '<' character. 与元素类型“变量”关联的属性“类型”的值不得包含“ <”字符。

I've searched the web and found people use &gt; 我搜索了网络,发现人们在使用&gt; for > and &lt; 用于>&lt; for < as a fix. 用于<作为修复。

在此处输入图片说明

Questions 问题

  1. Is this supposed to happen ? 这应该发生吗? If yes why it's not mentioned in the docs ? 如果是,为什么未在文档中提及?

  2. Is there any fix for this, where I can write the layout as given in the official docs? 有什么解决办法,我可以在其中编写官方文档中给出的布局? (without using corresponding html entity characters) (不使用相应的html实体字符)

There's unlikely to be a change to this because layout files are still XML, this isn't really the fault of Android or DataBinding, you're going to need to use appropriate encoding for HTML entities within an XML document. 由于布局文件仍然是XML,因此不太可能对此进行更改,这并不是Android或DataBinding的真正错,您将需要对XML文档中的HTML实体使用适当的编码。

Using &lt; 使用&lt; isn't that terrible as a fix, as far as resolutions go, but if you'd rather avoid using it, then it may be an option to simplify your binding expressions to move logic away from the layout and into your variables. 就分辨率而言,修复并不是那么糟糕,但是如果您不想使用它,那么它可能是简化绑定表达式以将逻辑从布局移到变量中的一种选择。

The current advised method of doing so is with a ViewModel, which can be bound to the layout and expose observable LiveData values. 当前建议使用的方法是使用ViewModel,可以将其绑定到布局并公开可观察的LiveData值。

I can't give you a reason for it not being in the documentation besides that it's probably just not advised to do so. 除了可能不建议这样做之外,我无法为您提供不在文档中的原因。

Now they've updated the documentation 现在他们更新了文档

在此处输入图片说明


在此处输入图片说明

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

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