Android数据绑定无法使用View’android:tag’属性

Android数据绑定无法使用View’android:tag’属性,第1张

概述试图在我的项目中使用新的 Android数据绑定,但是在尝试将’android:tag’属性设置为某个自定义变量时出现错误. 我的menu_item.xml文件: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:tools="http://schemas.android.com/tools" xmlns:androi 试图在我的项目中使用新的 Android数据绑定,但是在尝试将’androID:tag’属性设置为某个自定义变量时出现错误.

我的menu_item.xml文件:

@H_403_4@<?xml version="1.0" enCoding="utf-8"?> <layout xmlns:tools="http://schemas.androID.com/tools" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <data> <variable name="menuItem" type="com.example.MenuItem" /> </data> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="horizontal" androID:tag="@{menuItem}" tools:ignore="UseCompoundDrawables"> <!--suppress AndroIDUnkNownAttribute --> <ImageVIEw androID:ID="@+ID/icon" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:imageResource="@{menuItem.itemType.drawableID}" /> <TextVIEw androID:ID="@+ID/displayname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@{menuItem.itemType.displaynameID}" /> </linearLayout></layout>

我的MenuItem类:

@H_403_4@public class MenuItem { public final ItemType itemType; public MenuItem(ItemType itemType) { this.itemType = itemType; }}

MenyItembinding.java的一部分:

@H_403_4@public MenuItembinding(VIEw root) { super(root,0); final Object[] bindings = mapBindings(root,3,sIncludes,sVIEwsWithIDs); this.displayname = (androID.Widget.TextVIEw) bindings[2]; this.displayname.setTag(null); this.icon = (androID.Widget.ImageVIEw) bindings[1]; this.icon.setTag(null); this.mboundVIEw0 = (androID.Widget.linearLayout) bindings[0]; this.mboundVIEw0.setTag(root.getResources().getString(com.myApp.R.string.@{menuItem})); setRoottag(root); invalIDateall(); }

当尝试设置绑定视图的Tag时,错误在生成的类中.

任何想法如何解决这个问题?最好不要使用自定义linearLayout来支持这一点.

解决方法 那是一个错误.我们还没有尝试过数据绑定标签,主要是因为标签很特殊.

在ICS之前定位设备时,AndroID数据绑定会接管布局最外层元素的标记.此标记主要用于绑定生命周期,由DataBindingUtil.findBinding()和DataBindingUtil.getBinding()使用.

因此,由于数据绑定不适用于标记,因此唯一的解决方法是不向linearLayout提供标记或提供固定标记或资源字符串.如果您的目标是ICS及更高版本,则在绑定布局后重新分配标记是有效的:

@H_403_4@MenuItembinding binding = MenuItembinding.inflate(layoutInflater);binding.getRoot().setTag(menuItem);

您还可以为新属性创建BindingAdapter:

@H_403_4@@BindingAdapter("specialTag")public static voID setSpecialTag(VIEw vIEw,Object value) { vIEw.setTag(value);}

然后在你的布局中使用它:

@H_403_4@<linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="horizontal" app:specialTag="@{menuItem}" tools:ignore="UseCompoundDrawables"/>

这将允许您使用findVIEwByTag()和您期望的所有其他内容.

但是,如果您以Honeycomb和早期设备为目标,这将不起作用.没有绕过那个.你可能想做这样的事情:

@H_403_4@@BindingAdapter("specialTag")public static voID setSpecialTag(VIEw vIEw,Object value) { vIEw.setTag(R.ID.app_tag,value);}

使用该方法时,您将无法使用findVIEwByTag,但它将在您使用视图时存储您想要的任何值.但我们不使用Honeycomb及更早版本的ID标签的原因是使用ID’d标签时存在内存泄漏,所以不要这样做.

我希望这有帮助.我将在内部提交一个BUG来支持数据绑定的androID:标签.

总结

以上是内存溢出为你收集整理的Android数据绑定无法使用View’android:tag’属性全部内容,希望文章能够帮你解决Android数据绑定无法使用View’android:tag’属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134713.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存