如果在webview中有触摸事件,停止触摸事件被ViewPager拦截

我有一个显示碎片页面的ViewPager 。 每个片段都是显示离线网站的WebView。 其中一些网页浏览器中的JavaScript刷卡事件,使他们的工作。

我想实现这样的,如果webview没有滑动事件(在JavaScript内),然后触摸事件传递给ViewPager ,如果不是它被消费的networkingViewPager

我试图覆盖ViewPagers

 @Override public boolean onInterceptTouchEvent(MotionEvent event) { return (this.swipeable) ? super.onInterceptTouchEvent(event) : false; } 

然而,这阻止了ViewPager滑动(如预期),但也阻止了滑动事件到达webview,所以我有两个不希望的结果。

有什么办法将触摸事件传递给webView,然后如果它没有被消耗在片段传递给ViewPager

如果我在我的MyViewPager犯了一个错误,我有代码:

 public class MyViewPager extends ViewPager implements GestureDetector.OnGestureListener { private static final String TAG = MyViewPager.class.getSimpleName(); private boolean swipeable = true; private float lastX = 0; private long lastTime = 0 ; private GestureDetector mGestureDetector; private boolean mScrolling = false; public MyViewPager(Context context) { super(context); mGestureDetector = new GestureDetector(context, this); } public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, this); } public void setSwipeable(boolean swipeable) { this.swipeable = swipeable; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { /* float speed = 0; if(event.getAction() == MotionEvent.ACTION_MOVE) { if(lastX!=0) { speed = (event.getRawX()- lastX) / (System.currentTimeMillis()-lastTime); if(speed < 0.0 ){speed *=-1;} Log.e(TAG, "Move at speed ->"+speed); if(speed >0.5){return false;} } lastX = event.getRawX(); lastTime = System.currentTimeMillis(); return true; }*/ // return true; return (this.swipeable) ? super.onInterceptTouchEvent(event) : false; } @Override public boolean onTouchEvent(MotionEvent event) { Log.i(TAG, "onTouch event"); // mGestureDetector.onTouchEvent(event); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velX, float velY) { Log.i(TAG, "flinging"); return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distX, float distY) { float displacement = distX; return false; } // Unused Gesture Detector functions below @Override public boolean onDown(MotionEvent event) { return false; } @Override public void onLongPress(MotionEvent event) { // we don't want to do anything on a long press, though you should probably feed this to the page being long-pressed. } @Override public void onShowPress(MotionEvent event) { // we don't want to show any visual feedback } @Override public boolean onSingleTapUp(MotionEvent event) { // we don't want to snap to the next page on a tap so ignore this return false; } 

}

我现在有两个解决这个问题的方法是扩展一个ViewPager类并覆盖

 public boolean onInterceptTouchEvent(MotionEvent event) { // code } 

然而更好的解决scheme是覆盖

 protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { return true; } 

为了完整,这里是整个class级

 public class ZoltansViewPager extends ViewPager { private static final String TAG = ZoltansViewPager.class.getSimpleName(); private boolean swipeable = true; private float lastX = 0; private long lastTime = 0 ; private GestureDetector mGestureDetector; private boolean mScrolling = false; public ZoltansViewPager(Context context) { super(context); } public ZoltansViewPager(Context context, AttributeSet attrs) { super(context, attrs); } public void setSwipeable(boolean swipeable) { this.swipeable = swipeable; } @Override protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { Log.i(TAG , " boolean checkV ->"+checkV + ", int dx-> "+dx+", int x->"+x+", int y->"+y); if(x <300 || x >1700 ){return true;} return false; } } 

希望它能帮助别人