Android数据绑定无法使用View'android:tag'属性 [英] Android data binding not working with View 'android:tag' property

查看:170
本文介绍了Android数据绑定无法使用View'android:tag'属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在我的项目中使用新的Android数据绑定,但是当尝试将android:tag属性设置为某个自定义变量时,我收到错误。

Trying to use the new Android Data Binding in my project, but I'm getting an error when trying to set the 'android:tag' property to some custom variable.

我的menu_item.xml文件:

My menu_item.xml file:

<?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类:

My MenuItem class:

public class MenuItem {

    public final ItemType itemType;

    public MenuItem(ItemType itemType) {
        this.itemType = itemType;
    }

}

部分遗传的MenyItemBinding.java :

Part of the genetated 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();
    }

在生成的类中,当尝试设置绑定视图。

And the error is in the generated class, when trying to set the Tag of the bound view.

任何想法如何解决?最好不要使用自定义的LinearLayout来支持这个。

Any ideas how to get around this? Preferably, not to use a custom LinearLayout to support this.

推荐答案

这是一个错误。我们还没有尝试数据绑定标签,主要是因为标签是特殊的。

That is a bug. We haven't tried data binding tags, mostly because tags are special.

当定位设备ICS之前,Android数据绑定接管最外层元素的标签布局。该标签主要用于绑定生命周期,由 DataBindingUtil.findBinding() DataBindingUtil.getBinding()

When targeting devices pre-ICS, Android data binding takes over the tag of the outermost element of the layout. This tag is used for mostly for binding lifecycle and is used by DataBindingUtil.findBinding() and DataBindingUtil.getBinding().

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

So, since data binding isn't working on tags, the only work-around is to not supply a tag to your LinearLayout or supply a fixed tag or resource string. If you are targeting ICS and above, it is valid to reassign the tag after binding the layout:

MenuItemBinding binding = MenuItemBinding.inflate(layoutInflater);
binding.getRoot().setTag(menuItem);

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

You can also create a BindingAdapter for a new attribute:

@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()和您期望的其他所有内容。

This will allow you to use findViewByTag() and all of the other things you expect.

但是,如果您定位到Honeycomb和早期设备,这将无法正常工作。没有得到解决。你可能会想这样做:

However, this will NOT work if you target Honeycomb and earlier devices. There is no getting around that. You may be tempted to do something like this:

@BindingAdapter("specialTag")
public static void setSpecialTag(View view, Object value) {
    view.setTag(R.id.app_tag, value);
}

您将无法使用 findViewByTag ,但是当您使用视图时,它会存储您想要的任何值。 但是我们不使用Honeycomb和更早版本的ID'd标签的原因是使用ID'd标签时存在内存泄漏,所以不要这样做。

You won't be able to use findViewByTag with that approach, but it will store whatever value you want when you use your view. But the reason we don't use ID'd tags with Honeycomb and earlier is that there is a memory leak when using ID'd tags, so don't do it.

我希望这有帮助。我会在内部提交一个bug来支持数据绑定的android:tags。

I hope this helps. I'll file a bug internally to support data bound android:tags.

这篇关于Android数据绑定无法使用View'android:tag'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆