我有一个自定义的 Presenter
类,我似乎无法绕过必须将 android.view.View
对象作为参数传递到我想要执行的方法中用户点击按钮,即使我不需要 View
对象。使用 DataBinding
,是否可以将 onClick
事件绑定(bind)到没有 XML
参数的方法?
<layout>
<data>
<variable
name="presenter"
type="com.somepackage.Presenter"/>
</data>
...
android:onClick="@{ presenter.onClick }"
...
</layout>
在 Presenter
类中,我必须具有以下签名,否则代码无法编译:
...
public void onClick( @NonNull View ignored ) {
//do something that doesn't use the view parameter
}
...
我想做的是在我的 Presenter
calss 中有一个没有 View 作为参数的方法签名:
...
public void onClick() {
//do something that doesn't use the view parameter
}
...
这可能吗?
最佳答案
在你的xml中你必须做
android:onClick="@{() -> presenter.onClick() }"
调用你的方法
关于android - 使用 DataBinding 的无参数 OnClick 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517671/