Android数据绑定(bind)问题: Missing return statement in generated code while using with a custom view?

标签 android kotlin android-databinding

我在我当前的应用程序中使用数据绑定(bind),到目前为止一切顺利。但是,当我尝试将它与我为自定义 View 编写的自定义数据绑定(bind)适配器一起使用时,如标题所示,我从自动生成的文件中收到错误消息,缺少返回语句。当我仅在一个 View 上使用此数据绑定(bind)时不会发生此错误,但不止一个会出现错误。下面是我的自定义 View 和适配器,以及在 xml 文件中的用法。我已经检查了有点重复的问题的答案,但它在我的情况下不起作用,并且没有足够的解释。

class NeonTextView(context: Context, attrs: AttributeSet) : TextView(context, attrs) {

private val drawableClear: Drawable?
    get() = ContextCompat.getDrawable(context, R.drawable.ic_clear)

lateinit var actionMethod: () -> Unit
lateinit var clearMethod: () -> Unit
var hasActionMethod = false
var hasClearMethod = false

init {
    setupAttributes(attrs)
}

private fun setupAttributes(attrs: AttributeSet) {
    val typedArray =
        context.theme.obtainStyledAttributes(attrs, R.styleable.NeonTextView, 0, 0)

    hasActionMethod = typedArray.getBoolean(
        R.styleable.NeonTextView_hasActionMethod,
        false
    )
    hasClearMethod = typedArray.getBoolean(
        R.styleable.NeonTextView_hasClearMethod,
        false
    )
    typedArray.recycle()
}

override fun onTextChanged(
    text: CharSequence?,
    start: Int,
    lengthBefore: Int,
    lengthAfter: Int
) {
    text?.let { text ->
        drawableClear?.let {
            it.setBounds(0, 0, it.intrinsicWidth, it.intrinsicHeight)
        }
        setCompoundDrawablesWithIntrinsicBounds(
            null,
            null,
            if (text.isNotEmpty()) drawableClear!! else null,
            null
        )
    }
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
    event?.let {
        return when (it.action) {
            ACTION_DOWN -> return true
            ACTION_UP -> {
                if (compoundDrawables[2] == null && hasActionMethod) {
                    actionMethod()
                } else {
                    if (it.x > (width - paddingRight - compoundDrawables[2]!!.intrinsicWidth)) {
                        if (hasClearMethod) clearMethod()
                        text = ""
                    } else {
                        if (hasActionMethod) actionMethod()
                    }
                }
                performClick()
                true
            }
            else -> false
        }
    }.run {
        return false
    }
}

override fun performClick(): Boolean {
    super.performClick()
    return true
}
}

这是我在这个自定义 TextView 中使用的绑定(bind)方法的绑定(bind)适配器:

@BindingAdapter("actionMethod")
fun NeonTextView.setActionMethod(actionMethod: () -> Unit) {
    this.actionMethod = actionMethod
    this.hasActionMethod = true
}

@BindingAdapter("clearMethod")
fun NeonTextView.setClearMethod(clearMethod: () -> Unit) {
    this.clearMethod = clearMethod
    this.hasClearMethod = true
}

这是我在 xml 文件中应用的方式:

<com.android.platform.NeonTextView
                        android:id="@+id/textViewSectionField"
                        style="@style/HeaderTextView.SubHeader"
                        app:hasActionMethod="true"
                        app:actionMethod="@{() -> viewModel.getDepartmentList()}"/>

当我在 xml 中的多个 View 中使用此绑定(bind)时,为什么我从生成的文件中收到错误,有什么想法吗?

提前致谢。

最佳答案

问题在于 Java<->Kotlin 兼容性。在 Kotlin 中,如果你声明函数

interface Func {
    fun test(): Unit {
    }
}

并从 java 中使用它

class FuncImpl implements Func {
    @Override
    public Unit test() {
        return Unit.INSTANCE;
    }
}

请注意,在这种情况下,在 java 代码中您需要 return 语句。 lambda 也是如此。 因此,当您使用数据绑定(bind)从 xml 设置 lambda 时,它被视为 java 的 lambda,因此在这种情况下生成的代码没有得到正确处理。

关于Android数据绑定(bind)问题: Missing return statement in generated code while using with a custom view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692274/

相关文章:

java - 如何让多个 Activity 使用相同的跟踪 Activity ?

Kotlin 如何在 kotlin 中使用 java 流 .map() 来映射不同的对象响应

android - 如何正确地将数字绑定(bind)到 Android editText

android - 如何在没有游戏商店的情况下将通知发送到手机(例如华为)

android - 如何在 IntelliJ IDEA 中添加库(android-support-v7-appcompat)

spring-boot - Spring Boot:是否可以将.war中的CSS,JS和模板文件分开?

kotlin - Kotlin 数据类是否默认可序列化?

android - 数据绑定(bind) BR 在我的 mvvm 项目中的 notifyPropertyChanged 方法中不起作用

Android 数据绑定(bind)微调器适配器

java - Android中从URL读取XML文件