大佬教程收集整理的这篇文章主要介绍了Android数据绑定无法使用View’android:tag’属性,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:tools="http://scheR_391_11845@as.android.com/tools" xmlns:android="http://scheR_391_11845@as.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类:
public class MenuItem { public final ItemType itemType; public MenuItem(ItemType itemTypE) { this.itemType = itemType; } }@H_873_2@menyItemBinding.java的一部分:
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(); }
在ICS之前定位设备时,Android数据绑定会接管布局最外层元素的标记.此标记主要用于绑定生命周期,由DataBindingUtil.findBinding()和DataBindingUtil.getBinding()使用.
因此,由于数据绑定不适用于标记,因此唯一的解决方法是不向LinearLayout提供标记或提供固定标记或资源字符串.如果您的目标是ICS及更高版本,则在绑定布局后重新分配标记是有效的:
@H_398_4@menuItemBinding binding = MenuItemBinding.inflate(layoutInflater); binding.getRoot().setTag(menuItem);您还可以为新属性创建BindingAdapter:
@BindingAdapter("specialTag") public static void setSpecialTag(View view,Object value) { view.setTag(value); }
然后在你的布局中使用它:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" app:specialTag="@{menuItem}" tools:ignore="UseCompoundDrawables"/>
这将允许您使用findViewByTag()和您期望的所有其他内容.
但是,如果您以Honeycomb和早期设备为目标,这将不起作用.没有绕过那个.你可能想做这样的事情:
@BindingAdapter("specialTag") public static void setSpecialTag(View view,Object value) { view.setTag(R.id.app_tag,value); }
使用该方法时,您将无法使用findViewByTag,但它将在您使用视图时存储您想要的任何值.但我们不使用Honeycomb及更早版本的ID标签的原因是使用ID’d标签时存在内存泄漏,所以不要这样做.
以上是大佬教程为你收集整理的Android数据绑定无法使用View’android:tag’属性全部内容,希望文章能够帮你解决Android数据绑定无法使用View’android:tag’属性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。