Reputation: 2037
I has one activity (ContactActivity.kt) that contain only one webView component. Activity write on Kotlin.
I want to replace webView with my custom webView (ObservableWebView.java).
So here code (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
}
})
}
}
}
}
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);
}
}
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
Upvotes: 2
Views: 3235
Reputation: 89608
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.
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.
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.
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 {}
Upvotes: 2
Reputation: 20128
Why not just call
observableWebView.setOnLongClickListener
It looks like you're trying to "invoke" a method on observableWebView
, but it's not a function type?
Upvotes: 0