The following is a real working solution which will scrollWebView
On a horizontal swipe as long as it can scroll. If
WebView
Cannot further scroll, the next horizontal swipe will be consumed by
ViewPager
To switch the page.
Extending
WebView
With API-Level 14 (ICS)View
MethodcanScrollHorizontally()
Has been introduced, which we need to solve the problem. If you develop only for ICs or abve 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.
To do so simply derive your own class fromWebView
:
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 < 0) { return offset > 0; } else { return offset < range - 1; } }}
Important:Remember to reference yourExtendedWebView
Inside your layout file instead of the standard
WebView
.
Extending
ViewPager
Now you need to extendViewPager
To handle horizontal swipes correctly. This needs to be done in any case -- no matter whether you are using ICS or not:
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); } }}
Important:Remember to reference yourWebViewPager
Inside your layout file instead of the standard
ViewPager
.
That's it!
Update 2012/07/08:I 've recently noticed that the stuff shown abve seems to be no longer required when using the "current" Implementation of
ViewPager
. The "current" implementation seems to check the sub views correctly before capturing the scroll event on it's own (see
canScroll
MethodViewPager
Here). don't know exactly, when the implementation has been changed to handle this correctly -- I still need the code abve on Android Gingerbread (2.3.x) and before.
Share | edit | flag |
Edited Jul 8 at 11: 13 |
Answered Mar 29 at 13:07Sven 1, 272821 |