【问题标题】:Parameterless OnClick method call with DataBinding使用 DataBinding 调用无参数 OnClick 方法
【发布时间】:2018-09-06 03:28:50
【问题描述】:

我有一个自定义的Presenter 类,我似乎无法绕过将android.view.View 对象作为参数传递给我希望在用户单击按钮时执行的方法,即使我不需要@ 987654323@ 对象。使用DataBinding,是否可以将onClick 事件绑定到没有来自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
}
...

这可能吗?

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    在你的xml中你必须做 android:onClick="@{() -&gt; presenter.onClick() }" 调用你的方法

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2019-03-12
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多