gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:56:01 36 4
gpt4 key购买 nike

我在我当前的应用程序中使用数据绑定(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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com