mar*_*dan 6 data-binding android
试图在我的项目中使用新的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>
Run Code Online (Sandbox Code Playgroud)
我的MenuItem类:
public class MenuItem {
public final ItemType itemType;
public MenuItem(ItemType itemType) {
this.itemType = itemType;
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
当尝试设置绑定视图的Tag时,错误在生成的类中.
任何想法如何解决这个问题?最好不要使用自定义LinearLayout来支持这一点.
Geo*_*unt 14
那是一个错误.我们还没有尝试过数据绑定标签,主要是因为标签很特殊.
在ICS之前定位设备时,Android数据绑定会接管布局最外层元素的标记.此标记主要用于绑定生命周期,并由DataBindingUtil.findBinding()和使用DataBindingUtil.getBinding().
因此,由于数据绑定不适用于标记,因此唯一的解决方法是不向LinearLayout提供标记或提供固定标记或资源字符串.如果您的目标是ICS及更高版本,则在绑定布局后重新分配标记是有效的:
MenuItemBinding binding = MenuItemBinding.inflate(layoutInflater);
binding.getRoot().setTag(menuItem);
Run Code Online (Sandbox Code Playgroud)
您还可以为新属性创建BindingAdapter:
@BindingAdapter("specialTag")
public static void setSpecialTag(View view, Object value) {
view.setTag(value);
}
Run Code Online (Sandbox Code Playgroud)
然后在你的布局中使用它:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:specialTag="@{menuItem}"
tools:ignore="UseCompoundDrawables"/>
Run Code Online (Sandbox Code Playgroud)
这将允许您使用findViewByTag()和您期望的所有其他事项.
但是,如果您以Honeycomb和早期设备为目标,这将不起作用.没有绕过那个.你可能想做这样的事情:
@BindingAdapter("specialTag")
public static void setSpecialTag(View view, Object value) {
view.setTag(R.id.app_tag, value);
}
Run Code Online (Sandbox Code Playgroud)
您将无法使用findViewByTag该方法,但它将在您使用视图时存储您想要的任何值.但我们不使用Honeycomb及更早版本的ID标签的原因是使用ID'd标签时存在内存泄漏,所以不要这样做.
我希望这有帮助.我将在内部提交一个bug来支持数据绑定的android:标签.
| 归档时间: |
|
| 查看次数: |
4204 次 |
| 最近记录: |