Android 自定义视图上具有自定义侦听器的数据绑定

Android 自定义视图上具有自定义侦听器的数据绑定,android,data-binding,android-databinding,Android,Data Binding,Android Databinding,我试图用新的Android数据绑定库绑定自定义视图上的事件,但遇到了一个问题 以下是我的自定义视图的相关部分: public class SuperCustomView extends FrameLayout { private OnToggleListener mToggleListener; public interface OnToggleListener { void onToggle(boolean switchPosition); }

我试图用新的Android数据绑定库绑定自定义视图上的事件,但遇到了一个问题

以下是我的自定义视图的相关部分:

public class SuperCustomView extends FrameLayout {
    private OnToggleListener mToggleListener;

    public interface OnToggleListener {
        void onToggle(boolean switchPosition);
    }

    public void setOnToggleListener(OnToggleListener listener) {
        mToggleListener = listener;
    }
    .../...
 }
我正在尝试使用此自定义视图并将
ontogle
事件与以下内容绑定:

<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"/>
我在编译时遇到此错误:

> 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:ontogle
而不是
app:ontogle
,但得到了相同的错误

在阅读时,我感觉我可以将任何方法从控制器连接到
onToggle
事件

框架是否将
控制器.toggleStrokeSimulation
方法包装到
超级定制视图.ontogleListener
?关于框架提供的现有
onClick
背后的魔法有什么提示吗

@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,如:

 <com.androidbolts.databindingsample.model.SuperCustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:onToggleListener="@{controller.toggleStrokeLimitation}" />

现在不需要添加@BindingMethods注释

文档说明:

如果正确定义方法名称并遵循JavaBean格式,则在自定义视图上侦听器可能不需要
绑定方法
。 这里有一个例子

自定义视图类

public class CustomView extends LinearLayout {
    ...
    private OnCustomViewListener onCustomViewListener;

    ...
    public void setOnCustomViewListener(OnCustomViewListener onCustomViewListener) {
        this.onCustomViewListener = onCustomViewListener;
    }


    ....
    public interface OnCustomViewListener {
        void onCustomViewListenerMethod(int aNumber);
    }
}
XML

<...CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:onCustomViewListener="@{viewModel.viewModelListenerMethod}" // or use can use app:onCustomViewListener="@{viewModel::viewModelListenerMethod}"
    />

尝试为onToggle属性实现自定义setter,并确保controller.ToggleStrokelSimulation为OnToggleListener类型什么是
控制器.ToggleStrokelSimulation
?。似乎它是一个
对象
,您的setter需要一个
OnToggleListener
您是对的,我可以这样做,但我想将事件映射到一个自定义方法。如中所述,我将用我的推理编辑问题。这正是我想要的:)当我在接口中有两个方法时,即使我在自定义小部件类的顶部添加绑定方法,它也会给我错误。@AsadMukhtar每个接口使用一个方法。。。。是安卓的做事方式。在这里查看android的代码:很好的解决方案!如果OnCustomViewListener接口包含多个方法怎么办?
<...CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:onCustomViewListener="@{viewModel.viewModelListenerMethod}" // or use can use app:onCustomViewListener="@{viewModel::viewModelListenerMethod}"
    />
public class ViewModel extends BaseObservable{

    public void viewModelListenerMethod(int aNumber){
       // handle listener here
    }
}