Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it

Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it

我一直在尝试使用 android 数据绑定中的隐式属性侦听器 (reference) 来控制视图的可见性,它允许通过 id 访问视图并访问属性,如检查、可见等...,但是当尝试使用它时,它会抛出这样的错误

Error:(119, 29) Identifiers must have user defined types from the XML file. addTodo_switch_remind is missing it
<android.support.v7.widget.SwitchCompat
        android:id="@+id/addTodo_switch_remind"
        style="@style/MediumTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addTodo_space_project"
        android:text="@string/add_todo_remind_label"
        android:textOff="@string/generic_no_text"
        android:textOn="@string/generic_yes_text" />

    <android.support.v4.widget.Space
        android:id="@+id/addTodo_space_remind"
        style="@style/FormsSpacingStyle"
        android:layout_below="@+id/addTodo_switch_remind" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/addTodo_space_remind"
        android:orientation="vertical"
        android:padding="@dimen/grid_box_single"
        android:visibility="@{addTodo_switch_remind.checked ? View.VISIBLE : View.GONE}">

看起来隐式属性侦听器在表达式中使用时使用驼峰式大小写,多亏了这个post我弄明白了。

<!--Recurring Reminder -->
        <android.support.v7.widget.SwitchCompat
            android:id="@+id/addTodo_switch_remind"
            style="@style/MediumTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addTodo_space_project"
            android:text="@string/add_todo_remind_label"
            android:textOff="@string/generic_no_text"
            android:textOn="@string/generic_yes_text" />

        <android.support.v4.widget.Space
            android:id="@+id/addTodo_space_remind"
            style="@style/FormsSpacingStyle"
            android:layout_below="@+id/addTodo_switch_remind" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/addTodo_space_remind"
            android:orientation="vertical"
            android:padding="@dimen/grid_box_single"
            android:visibility="@{addTodoSwitchRemind.checked ? View.VISIBLE : View.GONE}">

为遇到相同问题的其他人记录文档

步骤 1:创建 BindingAdapter

@BindingAdapter("android:visibility")
public static void setVisibility(final View view, @IdRes int layourId) {
    SwitchCompat switcher = (SwitchCompat)view.getRootView().findViewById(layourId)
    switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            view.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        }
    }
}

步骤 2: 在你的数据绑定数据部分导入 R class layout.xml :

<data>
     <import type="example.package.R"/>
</data>

第 3 步:像这样将自定义视图绑定到您的切换器:

<android.support.v7.widget.SwitchCompat
    android:id="@+id/addTodo_switch_remind"/>

<LinearLayout
    android:visibility="@{R.id.addTodo_switch_remind">

当您在 .xml 文件中使用 View.VISIBLE / View.GONE 时,您应该通过添加 [=14 来导入 View 类型=] 在数据部分,如下所示:

<data>
    <import type="android.view.View"/>

    <variable
        name="viewModel"
        type="xx.xx.MyViewModel"/>
</data>