24

我正在尝试使用新的 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关于框架提供的现有魔法背后的任何暗示?

4

2 回答 2

26
@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 注释。

文档说: “某些属性具有名称不匹配的设置器。对于这些方法,可以通过 BindingMethods 注释将属性与设置器关联。这必须与类关联并包含 BindingMethod 注释,每个重命名的方法都有一个。 "

于 2015-09-22T11:03:34.367 回答
8

如果您按照 Java bean 格式正确定义方法名称,您可能不需要 BindingMethods在自定义视图上监听侦听器。这是一个例子

自定义视图类

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}"
    />

视图模型

public class ViewModel extends BaseObservable{

    public void viewModelListenerMethod(int aNumber){
       // handle listener here
    }
}
于 2018-01-26T08:31:19.097 回答