在 WebView 中检测滑动手势不允许在其中单击
Detecting swipe gestures in WebView disallows clicks in it
我有一个 WebView
,我正在为它实现一个 TouchListener
,它可以检测水平滑动。
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
}
这可以检测 WebView
上的滑动,但问题是,当用户点击 WebView
中的 link 时,WebView
没有响应.此外,WebView
的滚动不起作用。
What is the best way to handle clicks as well as to detect swipes on a WebView
?
除了滑动之外,您正在使用触摸事件,returning true
意味着您正在使用整个事件,因为它不会传播给父级。
@return 仅对您要消费的事件为真,否则为假
您可以像下面这样修改您的代码
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
}
我有一个 WebView
,我正在为它实现一个 TouchListener
,它可以检测水平滑动。
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
}
这可以检测 WebView
上的滑动,但问题是,当用户点击 WebView
中的 link 时,WebView
没有响应.此外,WebView
的滚动不起作用。
What is the best way to handle clicks as well as to detect swipes on a
WebView
?
除了滑动之外,您正在使用触摸事件,returning true
意味着您正在使用整个事件,因为它不会传播给父级。
@return 仅对您要消费的事件为真,否则为假
您可以像下面这样修改您的代码
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
}