Android:Kotlin:自定义webView-不能作为函数调用.找不到函数"invoke()" [英] Android: Kotlin: custom webView - cannot be invoked as a function. The function 'invoke()' is not found

查看:652
本文介绍了Android:Kotlin:自定义webView-不能作为函数调用.找不到函数"invoke()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动( ContactActivity.kt ),其中仅包含一个webView组件.活动写在Kotlin上.

I has one activity (ContactActivity.kt) that contain only one webView component. Activity write on Kotlin.

我想用自定义的webView( ObservableWebView.java )替换webView.

I want to replace webView with my custom webView (ObservableWebView.java).

所以这里的代码( MyActivity.kt ):

class ContactActivity : AppCompatActivity() {

        @SuppressLint("NewApi")
        override fun onCreate(savedInstanceState: Bundle?) {
            verticalLayout {
     val observableWebView = object : ObservableWebView(MainApp.getAppContext())
            observableWebView { // error here
                    setOnLongClickListener(object : View.OnLongClickListener {
                        override fun onLongClick(v: View): Boolean {
                            return true
                        }
                    })
                }
            }
       }
    }

我也有自定义的webView-ObservableWebView.java:

Also I has custom webView - ObservableWebView.java:

public class ObservableWebView extends WebView {
    private OnScrollChangedCallback mOnScrollChangedCallback;

    public ObservableWebView(final Context context) {
        super(context);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
    }

    public OnScrollChangedCallback getOnScrollChangedCallback() {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback) {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Impliment in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback {
        public void onScroll(int l, int t);
    }
}

但是在 MyActivity.kt 中出现编译错误:

But I get compile error in MyActivity.kt:

Error:(145, 13) Expression 'observableWebView' of type '<no name provided>' cannot be invoked as a function. The function 'invoke()' is not found

推荐答案

您可以使用这样的扩展功能来扩展Anko以支持您的自定义视图(请参阅文档

You can extend Anko to support your custom View with an extension function like this one (see the docs here):

inline fun ViewManager.observableWebView(theme: Int = 0, init: ObservableWebView.() -> Unit): ObservableWebView {
    return ankoView({ ObservableWebView(it) }, theme, init)
}

然后创建一个并设置一个侦听器如下:

Then creating one and setting a listener would look like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    verticalLayout {
        observableWebView {
            setOnLongClickListener {
                Log.d("WEBVIEW", "on long click")
                true
            }
            onLongClick {
                Log.d("WEBVIEW", "on long click")
            }
        }
    }
}

此代码展示了创建长单击侦听器的不同方法,当然您不需要两者.

This code shows off too different ways to create the long click listener, of course you do not need both.

第一个是经典方法,它使用WebViewsetOnLongClickListener函数和

The first one is the classic way, it uses WebView's setOnLongClickListener function, and SAM conversion to create a listener instance with a lambda. You have to return a boolean value here to indicate whether you've handled the event, or you want it to propagate.

使用onLongClick的第二种方法是使用Anko的一种方法.在0.10.0以上,将使用协程.在这里,您没有选择返回布尔值的选项.

The second way, using onLongClick is a way to do it with Anko. Above 0.10.0, this will use coroutines. Here, you don't get the option to return a boolean value.

(可选)如果您还希望能够使用observableWebView()语法,则可以创建另一个扩展程序,以委托给该先前的扩展程序:

Optionally, if you want to be able to use the observableWebView() syntax as well, you can create another extension that delegates to this previous extension:

inline fun ViewManager.observableWebView() = observableWebView {}

这篇关于Android:Kotlin:自定义webView-不能作为函数调用.找不到函数"invoke()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆