I want my ViewPager to be just a slide when using one finger. So I extended the class and implemented onInterceptTouchEvent as follows:
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getPointerCount() > 1) { return false; } return true; }
But getPointerCount () always returns "1", no matter how many points there are on the screen. I get the correct number when I redefine onTouchEvent, but when I do this, the error prevents the pager from working at all ( http://code.google.com/p/android/issues/detail?id=18990 ) when you pulled your first finger from multitouch: java.lang.IllegalArgumentException: pointerIndex out of range
How else can I do this?
EDIT:
The pointer counter error remains, but I managed to bypass the exception that gets into onTouchEvent.
I did this when I got the exception:
if(ev.getPointerCount() == 1) { return super.onTouchEvent(ev); } return false;
The problem is that when the first finger is pulled out of the multitouch, ViewPager onTouchEvent terminates the processing of the ACTION_UP event without first processing ACTION_DOWN. So I came up with this fix that avoids the exception and stops the ViewPager moving when you put your second finger:
private boolean moving = false; @Override public boolean onTouchEvent (MotionEvent ev) { int action = ev.getAction(); if(action == MotionEvent.ACTION_DOWN) { moving = true; } if(ev.getPointerCount() == 1) { if(moving) { return super.onTouchEvent(ev); } if(action == MotionEvent.ACTION_UP) { moving = false; } } else if(ev.getPointerCount() > 1 && moving) { ev.setAction(MotionEvent.ACTION_UP); moving = false; return super.onTouchEvent(ev); } return false; }
source share