DataBinding 库包含在没有变量的情况下不起作用
DataBinding library include don't work without variable
作为 George Mount 从 1.0-rc4 开始,我们在使用数据绑定时不再需要 include 中的变量:
buttons.xml:
<layout xmlns:andr...>
<Button
android:id="@+id/button"
...." />
main.xml:
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"/>
....
但我试过了,出现错误:
错误:(10, 31) 标识符必须具有来自 XML 文件的用户定义类型。 toolbarViewModel 缺少它
我有工具栏:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class="LoginPhoneFragmentBinding">
<variable
name="toolbarViewModel"
type="ru.mobileup.myalarm2.binding.ToolbarViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar"
android:id="@+id/toolbarBinding"/>
工具栏布局为:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ToolbarThemeOverlay"
app:navigationIcon="@{toolbarViewModel.navigationIconResId}"
app:navigationOnClickListener="@{toolbarViewModel.navigationOnClickListener}"
app:title="@{toolbarViewModel.title}"
app:menu="@{toolbarViewModel.menuResId}"
app:menuListener="@{toolbarViewModel.onMenuItemClickListener}"/>
怎么了?
注意:我知道通过传递的变量一切正常。我想弄清楚乔治提到的如何使用。
我认为您误解了乔治在链接中的回答 post;如果您想像在包含的工具栏布局中那样使用 @{toolbarViewModel.title}
引用它,您仍然需要该变量。
如果您在 build.gradle 中启用了 dataBinding 并将您的布局包装在额外的 <layout>
标记中,您将获得一个自动生成的 ViewDataBinding
class,其中包含已解析的对布局中具有 ID 的任何视图的引用(例如 binding.toolbar
,或类似的东西)。这不会自动为您绑定数据,但可以让您摆脱任何 findViewById
调用。
有关详细信息,请参阅 his blog post here。
作为 George Mount
buttons.xml:
<layout xmlns:andr...>
<Button
android:id="@+id/button"
...." />
main.xml:
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"/>
....
但我试过了,出现错误:
错误:(10, 31) 标识符必须具有来自 XML 文件的用户定义类型。 toolbarViewModel 缺少它
我有工具栏:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class="LoginPhoneFragmentBinding">
<variable
name="toolbarViewModel"
type="ru.mobileup.myalarm2.binding.ToolbarViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar"
android:id="@+id/toolbarBinding"/>
工具栏布局为:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ToolbarThemeOverlay"
app:navigationIcon="@{toolbarViewModel.navigationIconResId}"
app:navigationOnClickListener="@{toolbarViewModel.navigationOnClickListener}"
app:title="@{toolbarViewModel.title}"
app:menu="@{toolbarViewModel.menuResId}"
app:menuListener="@{toolbarViewModel.onMenuItemClickListener}"/>
怎么了?
注意:我知道通过传递的变量一切正常。我想弄清楚乔治提到的如何使用。
我认为您误解了乔治在链接中的回答 post;如果您想像在包含的工具栏布局中那样使用 @{toolbarViewModel.title}
引用它,您仍然需要该变量。
如果您在 build.gradle 中启用了 dataBinding 并将您的布局包装在额外的 <layout>
标记中,您将获得一个自动生成的 ViewDataBinding
class,其中包含已解析的对布局中具有 ID 的任何视图的引用(例如 binding.toolbar
,或类似的东西)。这不会自动为您绑定数据,但可以让您摆脱任何 findViewById
调用。
有关详细信息,请参阅 his blog post here。