【发布时间】:2020-01-14 12:59:14
【问题描述】:
我有两个相似的布局文件item_array_adapter 和item_recycler_adapter。第一个有一些特定的视图属性:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="ItemDataBinding">
<variable
name="item"
type="com.example.myapp.Item" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_text_view"
android:text="@{item.getName()}"
//Specific View Attributes />
</layout>
第二个完全一样,但有其他视图属性。第一个用于ArrayAdapter:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
int item = R.layout.item_array_adapter; //Different layout
ItemDataBinding binding = DataBindingUtil.inflate(inflater, item , parent, false);
第二个用于RecyclerAdapter:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
int item = R.layout.item_recycler_adapter; //Different layout
ItemDataBinding binding = DataBindingUtil.inflate(inflater, item , parent, false);
问题是我的应用程序失败并显示以下消息:
发现数据绑定错误: [数据绑定] {"msg":"无法写入 com.example.myapp.databinding.ItemDataBindingImpl","file":"D:\Projects\MyApp\app\src\main\res\layout\item_array_adapter.xml" ,"pos":[]}
如果我使用相同的布局,它可以工作。那么如何在具有不同布局的两个适配器中重用相同的ItemDataBinding 类?
【问题讨论】:
-
通过在数据标签中指定一个类名,您将强制库将该名称用于生成的类。是故意的吗?因为如果您为两个布局指定了相同的名称,则第二个将尝试生成具有相同名称的文件并且可能会失败。我想这就是你错误的原因。您可以重用数据绑定类,但不能像您尝试那样使用不同的布局。每个指定为数据绑定布局的不同布局都将生成一个特定的数据绑定类。
-
@OyaCanlı 非常感谢您对我的问题发表评论。我认为您应该将其添加为答案。
-
好的,完成!不客气!
标签: android android-recyclerview android-arrayadapter android-databinding