Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect pointer count onInterceptTouchEvent
    text
    copied!<p>I want my ViewPager to only slide when using a single finger. So I extended the class and implemented onInterceptTouchEvent like so:</p> <pre><code>@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getPointerCount() &gt; 1) { return false; } return true; } </code></pre> <p>But getPointerCount() is always returning '1', no matter how many points are on the screen. I get the correct number when I override onTouchEvent, but when I do that a bug is preventing the pager from working at all ( <a href="http://code.google.com/p/android/issues/detail?id=18990" rel="nofollow">http://code.google.com/p/android/issues/detail?id=18990</a> ) by throwning an app-killing exception when you raised your first finger out of multitouch: java.lang.IllegalArgumentException: pointerIndex out of range</p> <p>How else can I go about this?</p> <p><strong>EDIT:</strong></p> <p>The pointer count issue remains, but I was able to work around the exception that gets thrown in onTouchEvent.</p> <p>I was doing this when I got the exception:</p> <pre><code>if(ev.getPointerCount() == 1) { return super.onTouchEvent(ev); } return false; </code></pre> <p>The problem is that that when you raise your first finger out of multitouch, the ViewPager's onTouchEvent ends up processing the ACTION_UP event without first processing ACTION_DOWN. So I came up with this fix, which avoids the exception and will end the ViewPager's move when you put down a second finger:</p> <pre><code>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() &gt; 1 &amp;&amp; moving) { ev.setAction(MotionEvent.ACTION_UP); moving = false; return super.onTouchEvent(ev); } return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload