Android 数据绑定 "cannot find method"

Android Data Binding "cannot find method"

这在这一点上看起来很旧。我一直在评论 Android Data Binding Documentation

以及在 SO 上发布帖子,但似乎没有任何效果。

无论我如何格式化 XML,我得到的结果都是一样的。当我最初让这个工作(使用 lambda 而不传递参数)时,我经历了很多试验和错误。现在我需要在 onClick 中传递 View,我回到反复试验但没有任何功能。

MainViewModel.java

private void navClicked(@NonNull View view) {
    switch (view.getId()) {
        case R.id.btn1:
            break;
        case R.id.btn2:
            break;
    }
}

public void testBtn() {}

activity_main.xml

<data>
    <variable
        name="mainViewModel"
        type="com.example.viewmodel.MainViewModel" />
</data>

<!-- Works perfectly -->
<!-- however I would need a method for every button and that becomes redundant -->
<Button
    android:id="@+id/testBtn"
    android:onClick="@{() -> mainViewModel.testBtn()}"
    android:text="@string/testBtn" />

<!-- "msg":"cannot find method navClicked(android.view.View) in class com.example.viewmodel.MainViewModel" -->
<Button
    android:id="@+id/btn1"
    android:onClick="@{(v) -> mainViewModel.navClicked(v)}"
    android:text="@string/btn1" />

<!-- "msg":"cannot find method navClicked(android.view.View) in class com.example.viewmodel.MainViewModel" -->
<Button
    android:id="@+id/btn2"
    android:onClick="@{mainViewModel::navClicked}"
    android:text="@string/btn2" />

<!-- "msg":"Could not find identifier \u0027v\u0027\n\nCheck that the identifier is spelled correctly, and that no \u003cimport\u003e or \u003cvariable\u003e tags are missing." -->
<!-- This is missing (view) in the lambda - makes sense to fail -->
<Button
    android:id="@+id/btn3"
    android:onClick="@{() -> mainViewModel.navClicked(v)}"
    android:text="@string/btn2" />

navClicked() 是私有的...

// **denotes change, will not compile like that**
**public** void navClicked(@NonNull View view) {
    switch (view.getId()) {
        case R.id.btn1:
            break;
        case R.id.btn2:
            break;
    }
}

一旦我尝试了这个并且成功了:

XML:

android:onClick="@{() -> model.clickEvent(123)}"

型号:

public void clickEvent(int id) {...}

我使用字符串作为 id,但我认为它也适用于整数。