【问题标题】:Android data binding not working with View 'android:tag' propertyAndroid 数据绑定不适用于 View 'android:tag' 属性
【发布时间】:2015-08-05 07:37:02
【问题描述】:

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

我的 menu_item.xml 文件:

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

public class MenuItem {

    public final ItemType itemType;

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

}

部分生成的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();
    }

错误出现在生成的类中,尝试设置绑定视图的Tag。

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

【问题讨论】:

  • 你能打印 LogCat 吗?我从来没有直接在 xml 中设置标签,甚至不知道这是可能的。但是在这里,如果您尝试将 MenuItem 设置为标签,我猜 Android 会抱怨,因为它找不到 MenuItem 的空构造函数。
  • 这是一个编译时错误。请参阅上面生成的类的一部分。更具体地说,错误出现在 'this.mboundView0.setTag(root.getResources().getString(com.myApp.R.string.@{menuItem}));' .关于空的构造函数 - 我也将 menuItem 用于 TextView 和 ImageView 并且没有任何抱怨。

标签: android data-binding


【解决方案1】:

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

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

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

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 标记时存在 内存泄漏,所以不要这样做.

我希望这会有所帮助。我将在内部提交一个错误以支持数据绑定 android:tags。

【讨论】:

  • 有一件事我无法从文档中弄清楚 - 你在这里也提到了它。您将“setSpecialTag”方法放在哪里?我的印象是带有绑定的新属性需要自定义 LinearLayout。
  • @BindingAdapter 注解让数据绑定框架能够找到方法,因此这些静态方法可以在您的代码中的任何位置。我们建议使用与此类方法关联的特殊类。在这种情况下,您可以简单地使用“app:tag”属性,它会自动调用 setTag,因为“tag”与 setTag 方法匹配——您不需要任何 BindingAdapter。
  • @GeorgeMount "you could simply use "app:tag" attribute and it would call setTag automatically "。好吧,我试过了:app:tag='@{1}'。当您在代码中调用 getTag() 时,事情就很有趣了。你猜怎么着?它为您提供 ColorStateList。 ColorStateList,卡尔! BindingAdapter 方法效果很好,谢谢。
  • 这是一个错误!感谢收看。
  • 这是自动转换器的问题,将在下一个版本中修复。您可以使用 app:tag='@{(Integer)1}' 解决它
猜你喜欢
  • 2016-06-06
  • 2018-03-24
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多