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 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.

share | edit | flag edited Jul 8 at 11:13
answered Mar 29 at 13:07 sven
1,272 8 21

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. macOS Sierra安装Apache2.4+PHP7.0+MySQL
  2. MyBatis 如何写配置文件和简单使用
  3. win10免安装版本的MySQL安装配置教程
  4. Mac下安装mysql5.7 完整步骤(图文详解)
  5. ubuntu 15.04下mysql开放远程3306端口
  6. Navicat远程连接SQL Server并转换成MySQL
  7. 使用Mysql5.x以上版本出现报错#1929 Inco
  8. MySQL远程连接不上的解决方法
  9. linux下mysql开启远程访问权限 防火墙开
  10. mysql 5.7.16 安装配置方法图文教程(ubunt