Android Data Binding: Missing return statement in generated code when calling custom binding adapter more than once

馋奶兔 提交于 2020-05-14 17:36:30

问题


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
    })
}

In the layout I add app:onDrawableRightClick="@{() -> viewModel.doThing()}" to just one of the TextInputEditText and click run. Everything works, no problem.

Now I go back and add app:onDrawableRightClick="@{() -> viewModel.doOtherThing()}" to the second TextInputEditText. This time compilation fails with error: missing return statement.

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;
        }  
    }
}

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;
}

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?

I'm using Android Studio 3.4 Preview. Thanks all


回答1:


@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
        }
    }
}

try something like this i am using a custom interface for the listener(OnCompoundDrawableClickListener)



来源:https://stackoverflow.com/questions/53443784/android-data-binding-missing-return-statement-in-generated-code-when-calling-cu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!