28

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;
}
Spiff
  • 1,036
  • 1
  • 12
  • 18

2 Answers2

44

The following is a real working solution which will scroll the WebView on a horizontal swipe as long as it can scroll. If the WebView cannot further scroll, the next horizontal swipe will be consumed by the ViewPager to switch the page.

Extending the WebView

With API-Level 14 (ICS) the View method canScrollHorizontally() 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:

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 your ExtendedWebView inside your layout file instead of the standard WebView.

Extending the ViewPager

Now you need to extend the ViewPager 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 your WebViewPager inside your layout file instead of the standard ViewPager.

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 (see canScroll method of ViewPager here). 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.

snersesyan
  • 1,647
  • 17
  • 26
sven
  • 4,161
  • 32
  • 33
  • 1
    Does it work if an webview contains a java script with spin functionality like rotating globe? – Ads Mar 12 '13 at 11:00
  • @Ads: Haven't tried this -- you will need to try this on your own, sorry. – sven Mar 13 '13 at 08:48
  • 1
    Thanks! i used you solution for verical scroll inner WebViews at VerticalViewPager, based on Martin Sonc's project, for Froyo+. https://github.com/Sash0k/VerticalViewPager – Sash0k Jun 07 '13 at 09:49
  • 2
    When I try this computerHorizontalScrollRange() always returns 0. And computerHorizontalScrollRange() and computerHorizontalScrollExtent always return the same value (1200.) – android_student Dec 09 '14 at 21:31
  • I'm seeing the same as "android_student", such that computerHorizontalScrollRange() always returns 0, therefore it always returns false for "canScrollHor". Any idea why this is always returning 0? – se22as Nov 15 '16 at 12:25
  • I'm seeing the same as you, @se22as. Has anyone figured out a solution around this? – airowe Jan 24 '17 at 16:11
  • I'm seeing the same, always return 0. any solution for this? – Atiatul Maula May 12 '17 at 04:50
  • This is not working currently computeHorizontalScrollRange() EQUALS computeHorizontalScrollExtent() EQUALS getWidth(). so final int range == 0 is always true – snersesyan Feb 15 '18 at 11:35
  • Any updates here? Even 'canScroll' seems work, but it doesn't in fact... – Jaden Gu Jun 06 '18 at 09:45
-1

Although Sven mentioned for layout file I want to add detail. After you extend Webview and ViewPager classes,

Inside your activity you will cast to your extended class like this:

web = (MyWebView) findViewById(R.id.webview);

Inside your layout file like this:

<your.package.name.MyWebView
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
 />
trante
  • 33,518
  • 47
  • 192
  • 272