title: Android, canScrollVertically never called for ImageView
body: <p>I am using the <a href="https://github.com/MikeOrtiz/TouchImageView" rel="nofollow noreferrer">TouchImageView</a> by Mike Ortiz.</p>
  
  <p>Within it, <code>canScrollHorizontally</code> is overwritten:</p>
  
  <pre><code>@Override
  public boolean canScrollHorizontally(int direction) {
      matrix.getValues(m);
      float x = m[Matrix.MTRANS_X];
      float y = m[Matrix.MTRANS_Y];
  
      if (getImageWidth() &lt; viewWidth) {
          return false;
  
      } else if (x &gt;= -1 &amp;&amp; direction &lt; 0) {
          return false;
  
      } else if (Math.abs(x) + viewWidth + 1 &gt;= getImageWidth() &amp;&amp; direction &gt; 0) {
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>This works as expected. I can put the TouchImageView in a <code>ViewPager</code>, and scroll around the zoomed image, but also swipe right/left through the ViewPager when I reach the edges of the image.</p>
  
  <p>But what I intend to have is a vertical ViewPager nested within the regular ViewPager. The vertical ViewPager is modelled after <a href="https://stackoverflow.com/questions/13477820/android-vertical-viewpager">this</a> SO post:</p>
  
  <pre><code>public class VerticalViewPager extends ViewPager {
  
      public VerticalViewPager(Context context) {
          super(context);
          init();
      }
  
      public VerticalViewPager(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      private void init() {
          // The majority of the magic happens here
          setPageTransformer(true, new VerticalPageTransformer());
          // The easiest way to get rid of the overscroll drawing that happens on the left and right
          setOverScrollMode(OVER_SCROLL_NEVER);
      }
  
      private class VerticalPageTransformer implements ViewPager.PageTransformer {
  
          @Override
          public void transformPage(View view, float position) {
  
              if (position &lt; -1) { // [-Infinity,-1)
                  // This page is way off-screen to the left.
                  view.setAlpha(0);
  
              } else if (position &lt;= 1) { // [-1,1]
                  view.setAlpha(1);
  
                  // Counteract the default slide transition
                  view.setTranslationX(view.getWidth() * -position);
  
                  //set Y position to swipe in from top
                  float yPosition = position * view.getHeight();
                  view.setTranslationY(yPosition);
  
              } else { // (1,+Infinity]
                  // This page is way off-screen to the right.
                  view.setAlpha(0);
              }
          }
      }
  
      /**
       * Swaps the X and Y coordinates of your touch event.
       */
      private MotionEvent swapXY(MotionEvent ev) {
          float width = getWidth();
          float height = getHeight();
  
          float newX = (ev.getY() / height) * width;
          float newY = (ev.getX() / width) * height;
  
          ev.setLocation(newX, newY);
  
          return ev;
      }
  
      @Override
      public boolean onInterceptTouchEvent(MotionEvent ev){
          boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
          swapXY(ev); // return touch coordinates to original reference frame for any child views
          return intercepted;
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent ev) {
          return super.onTouchEvent(swapXY(ev));
      }
  
  }
  </code></pre>
  
  <p>With regular ImageViews, it works great. I can swipe left/right through the horizontal ViewPager, while also scrolling vertically through the nested ViewPager as needed. One modification to the vertical ViewPager (to allow horizontal scrolling from anywhere within the vertical ViewPager), is:</p>
  
  <pre><code>@Override
  public boolean canScrollHorizontally(int direction) {
      return false;
  }
  </code></pre>
  
  <p>Small change, works great.</p>
  
  <p>The trouble comes when the contents of the vertical ViewPager are the custom TouchImageViews. The right/left swiping still works great, but the vertical swiping only works when the right edge of the image is lined up with the right side of the device screen. This is due to the <code>canScrollHorizontally</code> override within the TouchImageView class.</p>
  
  <p>The <code>int direction</code> argument does not take x or y into account. A positive y swipe is indistinguishable from a positive x swipe.</p>
  
  <p>What I'd like to do is override the <code>canScrollVertically</code> method, to detect when a vertical swipe is occurring, but the method is never called. Even just a quick test like:</p>
  
  <pre><code>@Override
  public boolean canScrollVertically(int direction) {
      matrix.getValues(m);
  
      Toast.makeText(context, "vertical", Toast.LENGTH_SHORT).show();
  
  
      return true;
  }
  </code></pre>
  
  <p>is never called. Putting the same Toast message within the <code>canScrollHorizontally</code> method shows the message on every swipe (horizontal or vertical).</p>
  
  <p><strong>Why is <code>canScrollVertically</code> never called?</strong></p>
  
  <p>I'd like to detect when BOTH the image is bottomed-out in the view, and when the user is swiping up. When these two conditions are met, I can return <em>false</em> from the <code>canScrollHorizontally</code> and allow the vertical ViewPager to scroll properly.</p>
  
  <p>Thanks in advance for any help. Anyone know Mike Ortiz!?</p>