title: Android: nested ScrollViews and HorizontalScrollViews, interception
body: <p>I have a problem using ScrollViews inside of ScrollViews in Android. I know it's not kind of best practice, but I need to get it working.</p>
  
  <pre><code>public static void disableScrollOnParentHorizontalScrollView(View v) {  
      v.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              int action = event.getAction();
              switch (action) {
              case MotionEvent.ACTION_DOWN:
                  requestDisallowInterceptTouchEventByDirection(v, true, HorizontalScrollView.class);
                  break;
              case MotionEvent.ACTION_UP:
                  requestDisallowInterceptTouchEventByDirection(v, false, HorizontalScrollView.class);
                  break;
              }
              v.onTouchEvent(event);
              return true;
          }
      });
  }
  </code></pre>
  
  <p>I have the same method like above for vertical ScrollViews with ScrollView.class as a parameter.</p>
  
  <pre><code>private static void requestDisallowInterceptTouchEventByDirection(View v, boolean b, Class&lt;?&gt; clazz) {
      while (v.getParent() != null &amp;&amp; v.getParent() instanceof View) {
          if (clazz.isInstance(v.getParent())) {
              v.getParent().requestDisallowInterceptTouchEvent(b);
          }
          v = (View) v.getParent();
      }
  }
  </code></pre>
  
  <p>Here's a screenshot and I want to show you what exactly is the problem.</p>
  
  <ul>
  <li>A nested HorizontalScrollView inside of a ScrollView should scroll both vertically (parent) and horizontally (own).</li>
  <li>A nested ScrollView inside of a HorizontalScrollView should scroll both horizontally (parent) and vertically (own).</li>
  <li>A view inside of inside of a HorizontalScrollView and a vertical ScrollView should scroll both parents.</li>
  </ul>
  
  <p>This works in most cases.</p>
  
  <p><a href="https://i.stack.imgur.com/nXWea.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nXWea.png" alt="screenshot"></a></p>
  
  <p>The TextViews 5 and 9 are nested in ScrollViews, TextViews 6 and 10 are nested in HorizontalScrollViews.</p>
  
  <ul>
  <li>View #2 is scrolling vertically (2) and parent horizontally (1)</li>
  <li>View #3 is scrolling horizontally (3) and parent vertically (2)</li>
  <li>...</li>
  <li>View #6 is scrolling horizontally (6) and parent vertically (4)</li>
  <li>View #7 is scrolling parent horizontally (3) and parent vertically (4)</li>
  </ul>
  
  <p>So any view should scroll it's parent when it is not scrollable by itself.</p>
  
  <p>My problem is that a vertical scrolling view (5) inside a vertical scrolling view (4) does not scoll the outer vertical view's horizontal scrolling parent (3). </p>
  
  <p>Same problem in the other direction (View #10 (horizontal scrolling) does not scroll it's parent's (8, horizontal scrolling) parent (4, vertical scrolling), but the view right of view #10 scrolls both, 8 (horizontal) and 4 (vertical)).</p>
  
  <p>Seems like there are wrong interceptions on some views, but I don't find the problem.</p>
  
  <p>Can anyone help me?</p>
  
  <p>Best regards</p>