android - 自定义 View 上的自定义监听器的数据绑定(bind)

标签 android data-binding android-databinding

我正在尝试使用新的 Android 数据绑定(bind)库绑定(bind)自定义 View 上的事件,但遇到了问题。

这是我的自定义 View 的相关部分:

public class SuperCustomView extends FrameLayout {
    private OnToggleListener mToggleListener;

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

    public void setOnToggleListener(OnToggleListener listener) {
        mToggleListener = listener;
    }
    .../...
 }

我正在尝试使用此自定义 View 并将 onToggle 事件与以下内容绑定(bind):

<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 是 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 但得到相同的错误。

阅读 binding events section of the doc 时,我觉得我可以将 Controller 的任何方法连接到 onToggle 事件。

框架是否将 controller.toggleStrokeLimitation 方法包装到 SuperCustomView.OnToggleListener 中?关于框架提供的现有 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);
        }
    });
}

在我的 Controller 对象上:

 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 注解了。

文档说: "Some attributes have setters that don't match by name. For these methods, an attribute may be associated with the setter through BindingMethods annotation. This must be associated with a class and contains BindingMethod annotations, one for each renamed method. "

关于android - 自定义 View 上的自定义监听器的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32401150/

相关文章:

Android Studio : app theme different in preview, 运行设备和styles.xml

android - 如何在我的 Android 应用程序中使用摩托罗拉平板电脑 ET1 扫描仪获取二维码?

c# - 为什么找不到我的 ViewModel?

android - 尝试将数据绑定(bind)提供的上下文变量与 BaseObservable 一起使用时出错

Android DataBinding "??"符号和来自资源的参数化字符串。如何避免 'null' 文本?

java - 在 android studio 中按键盘上的 Enter 键提交包含 EditText 的 AlertDialog.Builder

android - 软键盘隐藏了部分全屏 webview

sqlite - 如何防止Sqlite未绑定(bind)参数解释为NULL

silverlight - 如何在 Silverlight 中将资源字符串绑定(bind)到 Xaml

java - 如何使用 android 数据绑定(bind)从 RealmList 获取项目