本文介绍了Android数据绑定-找不到& lt;& gt;的吸气剂接受参数类型' long'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在使用数据绑定,现在正尝试对具有'value'属性的自定义视图使用双向数据绑定.

I have been using data binding for the past few weeks and now am trying to use a two way data binding for a custom view with a 'value' attribute.

我的问题是在构建时出现以下错误.

My problem is that I get the following error when building.

现在我的理解是,绑定库将自动使用我的公共setter和getter,但是最令人困惑的部分是添加冗余的反向绑定适配器似乎可以解决问题?因此,我得到的印象是它使用我的setter而不需要适配器,但是getter并非如此?

Now it was my understanding that the binding library will automatically use my public setters and getters however the most confusing part is adding a redundant inverse binding adapter seems to solve the problem? So I get the impression that it is using my setter without needing an adapter but this is not the case for the getter?

如果有人可以对此有所了解,或者说在这种情况下通常是如何进行绑定的,那么将不胜感激.这是我的相关代码,请问您是否有任何疑问!

If someone could shed some light on this, or generally how the binding works in this instance it would be much appreciated. Here is my relevant code, please ask if you have any questions!


我的自定义视图

class DraglineView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    ...

    var value = 0L
        set(value) {

            draggedValue = value
            field = value
            invalidate()
        }

    ...
}


我在布局文件中的视图

<com.twisthenry8gmail.dragline.DraglineView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:increment="@{viewmodel.type.minIncrement}"
    app:minValue="@{viewmodel.type.minIncrement}"
    app:value="@={viewmodel.target}" />


我看似多余的适配器

@InverseBindingAdapter(attribute = "value")
@JvmStatic
fun getValueTest(draglineView: DraglineView): Long {

    return draglineView.value
}


我的属性已更改适配器

@BindingAdapter("valueAttrChanged")
@JvmStatic
fun setDraglineListener(draglineView: DraglineView, listener: InverseBindingListener) {

    draglineView.valueChangedListener = {

        listener.onChange()
    }
}

推荐答案

问题是数据绑定系统不知道视图何时更改.

The problem is that databinding system doesn't know when the view changes the value.

InverseBindingAdapter 不仅描述了如何检索 value ,但它还定义了可选的事件属性,该属性将收到一个 InverseBindingListener 实例.默认事件名称是带有"AttrChanged"后缀的属性名称.

InverseBindingAdapter not only describes how to retrieve the value from the view, but it also defines an optional event property which will receive an InverseBindingListener instance. The default event name is the attribute name suffixed with "AttrChanged".

现在让我们看一下您的setDraglineListener()适配器.它处理由InverseBindingAdapter添加的valueAttrChanged属性并接收InverseBindingListener.剩下的唯一事情是通过调用listener.onChange();

Now let's look at your setDraglineListener() adapter. It processes valueAttrChanged attribute added by InverseBindingAdapter and receives InverseBindingListener. The only thing is left is to notify the listener when the value is changed by calling listener.onChange();

这篇关于Android数据绑定-找不到&amp; lt;&amp; gt;的吸气剂接受参数类型&amp;#39; long&amp;#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:53