先说结论
嵌套后webview竖向滑动一卡一卡的,最后修改ViewPager2的最小切换页面边距降低切换灵敏度,使一些常规滑动不会被拦截
try {
final Field recyclerViewField = ViewPager2.class.getDeclaredField("mRecyclerView");
recyclerViewField.setAccessible(true);
final RecyclerView recyclerView = (RecyclerView) recyclerViewField.get(viewPager);
final Field touchSlopField = RecyclerView.class.getDeclaredField("mTouchSlop");
touchSlopField.setAccessible(true);
final int touchSlop = (int) touchSlopField.get(recyclerView);
touchSlopField.set(recyclerView, touchSlop*5);//通过获取原有的最小滑动距离 *n来增加此值
} catch (Exception ignore) {
}
处理完垂直滑动,又来了横向滑动
addJavascriptInterface WebView网页端通知原生是否需要横向滑动,设置boolean标志
webMoveX = (TextUtils.equals(event,"DOWN")||TextUtils.equals(event,"MOVE_X"));
重写onTouchEvent在webview需要滑动的时候通知父控件不要拦截事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (webMoveX) {
super.requestDisallowInterceptTouchEvent(true);
}
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
downFlag = true;
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(event.getX() - startX);
float dy = Math.abs(event.getY() - startY);
//首次为竖直滑动 后续滑动webview处理
if (downFlag && dy > dx){
super.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
downFlag = false;
break;
}
return super.onTouchEvent(event);
}
提供一个简易处理初始上下滑动WebView然后左右滑动触发ViewPager2切换冲突的方案
//重写webview onScrollChanged 优先响应webview内部滑动 如果webview内部滑动完成,则响应外部的滑动,把webview包含到下拉刷新后此方法不调用了,调用的是下拉刷新的该方法.待研究...
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//处理初始上下滑动WebView然后左右滑动触发ViewPager2切换
super.requestDisallowInterceptTouchEvent(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (webMoveX) {
super.requestDisallowInterceptTouchEvent(true);
}
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
downFlag = true;
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(event.getX() - startX);
float dy = Math.abs(event.getY() - startY);
//首次为竖直滑动且非下拉刷新 后续滑动webview处理
if (!pullToRefresh && downFlag && dy > dx){
super.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
downFlag = false;
break;
}
return super.onTouchEvent(event);
}