Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following is a real working solution which will scroll the <code>WebView</code> on a horizontal swipe as long as it can scroll. If the <code>WebView</code> cannot further scroll, the next horizontal swipe will be consumed by the <code>ViewPager</code> to switch the page.</p> <h1>Extending the <code>WebView</code></h1> <p>With API-Level 14 (ICS) the <code>View</code> method <code>canScrollHorizontally()</code> has been introduced, which we need to solve the problem. If you develop only for ICS or above you can directly use this method and skip to the next section. Otherwise we need to implement this method on our own, to make the solution work also on pre-ICS.</p> <p>To do so simply derive your own class from <code>WebView</code>:</p> <pre><code>public class ExtendedWebView extends WebView { public ExtendedWebView(Context context) { super(context); } public ExtendedWebView(Context context, AttributeSet attrs) { super(context, attrs); } public boolean canScrollHor(int direction) { final int offset = computeHorizontalScrollOffset(); final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent(); if (range == 0) return false; if (direction &lt; 0) { return offset &gt; 0; } else { return offset &lt; range - 1; } } } </code></pre> <p><strong>Important:</strong> Remember to reference your <code>ExtendedWebView</code> inside your layout file instead of the standard <code>WebView</code>.</p> <h1>Extending the <code>ViewPager</code></h1> <p>Now you need to extend the <code>ViewPager</code> to handle horizontal swipes correctly. This needs to be done in any case -- no matter whether you are using ICS or not:</p> <pre><code>public class WebViewPager extends ViewPager { public WebViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { if (v instanceof ExtendedWebView) { return ((ExtendedWebView) v).canScrollHor(-dx); } else { return super.canScroll(v, checkV, dx, x, y); } } } </code></pre> <p><strong>Important:</strong> Remember to reference your <code>WebViewPager</code> inside your layout file instead of the standard <code>ViewPager</code>.</p> <p><strong>That's it!</strong></p> <p><strong>Update 2012/07/08:</strong> I've recently noticed that the stuff shown above seems to be no longer required when using the "current" implementation of the <code>ViewPager</code>. The "current" implementation seems to check the sub views correctly before capturing the scroll event on it's own (see <code>canScroll</code> method of <code>ViewPager</code> <a href="https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/view/ViewPager.java" rel="nofollow noreferrer">here</a>). Don't know exactly, when the implementation has been changed to handle this correctly -- I still need the code above on Android Gingerbread (2.3.x) and before.</p>
 

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