与定制视图中的自定义侦听器的数据绑定 [英] Data bindings with custom listeners on custom view
问题描述
我正在尝试使用新的Android数据绑定库在自定义视图上绑定一个事件,但遇到问题。
I'm trying to bind an event on a custom view with the new Android data-binding library but run into an issue.
这是我的相关部分自定义视图:
Here's the relevant part of my custom view:
public class SuperCustomView extends FrameLayout {
private OnToggleListener mToggleListener;
public interface OnToggleListener {
void onToggle(boolean switchPosition);
}
public void setOnToggleListener(OnToggleListener listener) {
mToggleListener = listener;
}
.../...
}
我试图使用此自定义视图并绑定 onToggle
事件与以下:
I'm trying to use this Custom View and bind the onToggle
event with the following:
<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
是控制器上的一种方法:
Where toggleStrokeLimitation
is a method on the controller:
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
,但得到相同的错误。
I've tried to use android:onToggle
instead of app:onToggle
but get the same error.
阅读文档的绑定事件部分,我觉得我可以将控制器上的任何方法连接到 onToggle
event。
When reading the binding events section of the doc, I feel like I can wire any method off the controller to the onToggle
event.
框架是否将 controller.toggleStrokeLimitation
方法包含到 SuperCustomView.OnToggleListener
?任何关于由框架提供的现有 onClick
背后的魔法的提示
Does the framework wrap the controller.toggleStrokeLimitation
methods into a SuperCustomView.OnToggleListener
? Any hint on the kind of magic that is behind the existing onClick
provided by the framework?
推荐答案
@BindingMethods(@BindingMethod(type = SuperCustomView.class, attribute = "app:onToggle", method = "setOnToggleListener"))
public class SuperCustomView extends FrameLayout {
private OnToggleListener mToggleListener;
public interface OnToggleListener {
void onToggle(boolean switchPosition);
}
public void setOnToggleListener(OnToggleListener listener) {
mToggleListener = listener;
}
.../...
}
我的黑客测试代码是:
public void setOnToggleListener(final OnToggleListener listener) {
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toggle = !toggle;
listener.onToggle(toggle);
}
});
}
在我的控制器对象上:
public class MyController {
private Context context;
public MyController(Context context) {
this.context = context;
}
public void toggleStrokeLimitation(boolean switchPosition) {
Toast.makeText(context, "Toggle" + switchPosition, Toast.LENGTH_SHORT).show();
}
}
是啊!它工作
或者你可以使用xml,如:
Yeah!! it worked
Alternatively you can use xml like:
<com.androidbolts.databindingsample.model.SuperCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:onToggleListener="@{controller.toggleStrokeLimitation}" />
现在不需要添加@BindingMethods注释。
Now no need to add @BindingMethods annotation.
这篇关于与定制视图中的自定义侦听器的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!