lit*_*hen 6 android ontouchlistener android-webview android-recyclerview android-touch-event
我在WebView里面RecyclerView.为了获得平滑的滚动体验,当用户滚动时,RecyclerView将负责滚动(WebView不应滚动)当我只有一个触摸点并且正在垂直移动(向上和向下滚动)时我调用getParent().requestDisallowInterceptTouchEvent(false);内部webview#onTouchEvent(event).
private void handleSingleFingerTouch(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = ev.getX();
y1 = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = ev.getX();
y2 = ev.getY();
// -ve param for canScrollHorizontally is to check scroll left. +ve otherwise
if (Math.abs(x1 - x2) >= Math.abs(y1 - y2)
&& canScrollHorizontally((int) (x1 - x2))) {
// scrolling horizontally. Retain the touch inside the webView.
getParent().requestDisallowInterceptTouchEvent(true);
} else {
// scrolling vertically. Share the touch event with the parent.
getParent().requestDisallowInterceptTouchEvent(false);
}
x1 = x2;
y1 = y2;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
boolean multiTouch = ev.getPointerCount() > 1;
if (multiTouch) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
handleSingleFingerTouch(ev);
}
return super.onTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
只用一个bug就可以正常工作,我发现虽然RecyclerView(和webview)滚动并且我在里面触摸WebView,然后RecyclerView按预期停止滚动,然后如果我不抬起手指但是将手指放在屏幕上并尝试缩放,webview不会缩放,实际上它根本不会接收触摸事件.我必须抬起手指再次触摸才能放大.我知道这是因为getParent().requestDisallowInterceptTouchEvent(false);除非UI接收CANCEL或UP事件,否则不会取消.我试图实现一个getParent().requestDisallowInterceptTouchEvent(true);在多点触控发生时调用的接口.虽然它确实被调用,但似乎它不起作用.变焦还没有发生,onTouchEvent里面WebView仍然没有被触发.有什么想法解决这个问题?
所以基本上解决方案是覆盖onInterceptTouchEventrecyclerView
override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
// When recyclerview is scrolling this will stop scrolling and allow touch event passed to child views.
if (e.action == MotionEvent.ACTION_DOWN && this.scrollState == RecyclerView.SCROLL_STATE_SETTLING) {
this.stopScroll()
}
return super.onInterceptTouchEvent(e)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |