Android: extends webview and viewpager to achieve horizontal sliding in viewpager, similar to Gmail.

Source: Internet
Author: User

The following is a real working solution which will scrollWebViewOn a horizontal swipe as long as it can scroll. If
WebViewCannot further scroll, the next horizontal swipe will be consumed by
ViewPagerTo switch the page.

Extending WebView

With API-Level 14 (ICS)ViewMethodcanScrollHorizontally()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 yourExtendedWebViewInside your layout file instead of the standard
WebView.

Extending ViewPager

Now you need to extendViewPagerTo 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 yourWebViewPagerInside 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
canScrollMethodViewPager
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:07

Sven
1, 272821
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.