问题描述
我正在使用android数据绑定库和MVVM体系结构.在xml布局中,我定义了一个名为myModel的viewModel变量.布局中有几个TextInputEditText,我为此使用了以下自定义绑定适配器:
I am using the android data binding library and MVVM architecture. In the xml layout I define a variable named viewModel of type myViewModel. The layout has several TextInputEditText for which I used the following custom binding adapter:
//makes the drawable_right of the TextView clickable
@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("onDrawableRightClick")
inline fun TextView.setOnDrawableRightClick(crossinline f: () -> Unit) {
this.setOnTouchListener(View.OnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= this.right - this.paddingRight - this.compoundDrawables[DRAWABLE_RIGHT].bounds.width()) {
f()
return@OnTouchListener true
}
}
false
})
}
在布局中,我仅将app:onDrawableRightClick="@{() -> viewModel.doThing()}"
添加到TextInputEditText之一,然后单击运行.一切正常,没问题.
In the layout I add app:onDrawableRightClick="@{() -> viewModel.doThing()}"
to just one of the TextInputEditText and click run. Everything works, no problem.
现在我返回并将app:onDrawableRightClick="@{() -> viewModel.doOtherThing()}"
添加到第二个TextInputEditText.这次编译失败,并显示error: missing return statement
.
Now I go back and add app:onDrawableRightClick="@{() -> viewModel.doOtherThing()}"
to the second TextInputEditText. This time compilation fails with error: missing return statement
.
此代码块中的错误是在MyFragmentBindingImpl中(生成的):
The error is in MyFragmentBindingImpl (generated), in this block of code:
public final kotlin.Unit _internalCallbackInvoke(int sourceId ) {
switch(sourceId) {
case 1: {
// localize variables for thread safety
// viewModel
com.example.MyViewModel viewModel = mViewModel;
// viewModel != null
boolean viewModelJavaLangObjectNull = false;
viewModelJavaLangObjectNull = (viewModel) != (null);
if (viewModelJavaLangObjectNull) {
viewModel.doOtherThing();
}
return null;
}
case 2: {
// localize variables for thread safety
// viewModel
com.example.MyViewModel viewModel = mViewModel;
// viewModel != null
boolean viewModelJavaLangObjectNull = false;
viewModelJavaLangObjectNull = (viewModel) != (null);
if (viewModelJavaLangObjectNull) {
viewModel.doThing();
}
return null;
}
}
}
在开关之外既没有默认情况,也没有return语句.这会导致错误,但是我很确定在处理每种情况时都不需要使用默认大小写...总之,当我返回xml并删除其中一个侦听器绑定时,MyFragmentBindingImpl更改为:
There is neither a default case nor a return statement outside of the switch. This causes the error but I was pretty sure that the default case isn't necessary when every case is handled... Anyways, when I go back to xml and remove one of the listener bindings, MyFragmentBindingImpl changes to this:
public final kotlin.Unit _internalCallbackInvoke(int sourceId ) {
// localize variables for thread safety
// viewModel
com.example.MyViewModel viewModel = mViewModel;
// viewModel != null
boolean viewModelJavaLangObjectNull = false;
viewModelJavaLangObjectNull = (viewModel) != (null);
if (viewModelJavaLangObjectNull) {
viewModel.doThing();
}
return null;
}
编译器再次感到高兴,但是我需要多次使用绑定适配器.如何使库添加一个return语句?有解决方法吗?
The compiler is happy again, but I need to use the binding adapter more than once. How can I make the library add a return statement? Is there a workaround?
我正在使用Android Studio 3.4预览版.谢谢大家
I'm using Android Studio 3.4 Preview. Thanks all
推荐答案
@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("onDrawableEndClick")
fun setOnDrawableEndClick(view: TextView, listener: OnCompoundDrawableClickListener?) {
val padding = 10
if (listener != null) {
view.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
if (view.compoundDrawables[DRAWABLE_RIGHT] == null) return@setOnTouchListener false
else if (event.rawX >= (view.right - view.compoundDrawables[DRAWABLE_RIGHT].bounds.width() - padding)) {
listener.onDrawableEnd()
return@setOnTouchListener true
}
}
return@setOnTouchListener false
}
}
}
尝试这样的事情,我正在为侦听器(OnCompoundDrawableClickListener)使用自定义界面
try something like this i am using a custom interface for the listener(OnCompoundDrawableClickListener)
这篇关于Android数据绑定:多次调用自定义绑定适配器时,生成的代码中缺少返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!