Issue
I have a WebView and I am implementing a TouchListener to it which can detect horizontal swipe.
webView.setOnTouchListener { v, event ->
when ( event.action ) {
MotionEvent.ACTION_DOWN -> x1 = event.x
MotionEvent.ACTION_UP -> {
x2 = event.x
val distance = Math.abs( x2 - x1 )
if ( distance >= thresholdDistance ) {
if ( webView.canGoBack() ) {
webView.goBack()
}
}
}
}
true
}
This can detect swipes on the WebView, but the problem is, when the user clicks on a link in the WebView, the WebView does not respond. Also, the scrolling of the WebView doesn't work.
What is the best way to handle clicks as well as to detect swipes on a
WebView?
Solution
You are consuming the touch event even apart from swipe, returning true means that you are consuming the entire event, by what it won't get propagate to the parent.
@return True only for the event you suppose to consume, false otherwise
You can modify your code like below
webView.setOnTouchListener { v, event ->
when ( event.action ) {
MotionEvent.ACTION_DOWN -> x1 = event.x
MotionEvent.ACTION_UP -> {
x2 = event.x
val distance = Math.abs( x2 - x1 )
if ( distance >= thresholdDistance ) {
if ( webView.canGoBack() ) {
webView.goBack()
}
true
}
}
}
false
}
Answered By - Krishna Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.