我正在尝试使用新的 Android 数据绑定库在自定义视图上绑定事件,但遇到了问题。
这是我的自定义视图的相关部分:
public class SuperCustomView extends FrameLayout {
private OnToggleListener mToggleListener;
public interface OnToggleListener {
void onToggle(boolean switchPosition);
}
public void setOnToggleListener(OnToggleListener listener) {
mToggleListener = listener;
}
.../...
}
我正在尝试使用此自定义视图并将onToggle
事件与以下内容绑定:
<data>
<variable
name="controller"
type="com.xxx.BlopController"/>
</data>
<com.company.views.SuperCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onToggle="@{controller.toggleStrokeLimitation}"
app:custom_title="Blah"
app:custom_summary="Bloh"
app:custom_widget="toggle"/>
toggleStrokeLimitation
控制器上的方法在哪里:
public void toggleStrokeLimitation(boolean switchPosition) {
maxStrokeEnabled.set(switchPosition);
}
编译时出现此错误:
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:onToggle' with parameter type java.lang.Object. file:/path/to/androidapp/app/src/main/res/layout/fragment_stroke.xml loc:36:35 - 36:67 ****\ data binding error ****
我尝试使用android:onToggle
而不是app:onToggle
但得到相同的错误。
在阅读文档的绑定事件部分时,我觉得我可以将控制器的任何方法连接到onToggle
事件。
框架是否将controller.toggleStrokeLimitation
方法包装成SuperCustomView.OnToggleListener
? onClick
关于框架提供的现有魔法背后的任何暗示?