数据绑定传递带有特殊字符的 xml 元素

Data binding passing xml element with special characters

考虑以下 bindingAdapter

@BindingAdapter({"pager"})
public static void bindViewPagerTabs(final TabLayout view, final ViewPager pagerView)
{
    view.setupWithViewPager(pagerView, true);
}

xml 中的设置是:

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/card_control_tab_layout"
            style="@style/tab_in_toolbar_style"
            android:background="@color/colorPrimary"
            app:tabGravity="fill"
            app:pager="@{(pager_r)}"
            app:tabMode="scrollable" />

给出以下错误:

error: cannot find symbol class ActivityCardControlBindingImpl

但是,如果我将寻呼机 ID 更改为没有下划线或任何特殊字符(如 @+id/pager)的任何内容,它就可以正常工作,有什么正当理由吗?

如果您写 app:pager="@{(pagerR)}" 而不是 app:pager="@{(pager_r)}",它会起作用。你也不需要括号,所以你可以只写 app:pager="@{pagerR}".

编辑:

But if I changed the pager Id to anything without the underscore or any special characters like @+id/pager it works perfectly, any valid reason ?

我不知道其他特殊字符,但是当您在视图 ID 中使用下划线时,像 "pager_r" 这样的蛇形视图 ID 会通过数据绑定转换为驼峰变量,例如 "pagerR" .因此,正如我最初的回答所述,如果您使用 @+id/pager_r 您可以使用变量 pagerR.

我找到了与 Janosch Pelzer 相同的答案,但这里有更多解释