I’ve put a WebView loading an image inside a ViewPager. When I try to scroll the image horizontally I move over to the next view instead of scrolling the image.
Is it possible to make it scroll to the end of the image before moving over to the next view?
@Override
public Object instantiateItem(View view, int i) {
WebView webview = new WebView(view.getContext());
webview.setHorizontalScrollBarEnabled(true);
webview.loadUrl("http://www.site.with.an/image.gif");
((ViewPager) view).addView(webview, 0);
return webview;
}
The following is a real working solution which will scroll the
WebViewon a horizontal swipe as long as it can scroll. If theWebViewcannot further scroll, the next horizontal swipe will be consumed by theViewPagerto switch the page.Extending the
WebViewWith API-Level 14 (ICS) the
ViewmethodcanScrollHorizontally()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.To do so simply derive your own class from
WebView:Important: Remember to reference your
ExtendedWebViewinside your layout file instead of the standardWebView.Extending the
ViewPagerNow you need to extend the
ViewPagerto handle horizontal swipes correctly. This needs to be done in any case — no matter whether you are using ICS or not:Important: Remember to reference your
WebViewPagerinside your layout file instead of the standardViewPager.That’s it!
Update 2012/07/08: I’ve recently noticed that the stuff shown above seems to be no longer required when using the “current” implementation of the
ViewPager. The “current” implementation seems to check the sub views correctly before capturing the scroll event on it’s own (seecanScrollmethod ofViewPagerhere). 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.