Android: 扩展WebView 和ViewPager实现viewpager中的水平滑动,类似Gmail的效果
来源:互联网 发布:linux中rm命令 编辑:程序博客网 时间:2024/04/30 05:02
The following is a real working solution which will scroll the WebView
on a horizontal swipe as long as it can scroll. If theWebView
cannot further scroll, the next horizontal swipe will be consumed by theViewPager
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 standardWebView
.
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 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 theViewPager
. The "current" implementation seems to check the sub views correctly before capturing the scroll event on it's own (seecanScroll
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.
- Android: 扩展WebView 和ViewPager实现viewpager中的水平滑动,类似Gmail的效果
- 自定义ViewGroup实现类似ViewPager的滑动效果
- android viewpager和fragment相结合,实现菜单的滑动效果
- 使用ViewPager和GridView配合,实现GridView横向水平滑动的效果。
- Android自定义ViewPager:水平滑动弹性效果,侧滑刷新加载的ViewPager
- android Viewpager 左右滑动类似桌面的实现
- TabLayout 和ViewPager和Fragment的多页面滑动(主要实现一个水平的布局用来展示Tabs加上ViewPager实现联动效果)
- Android中使用ViewPager和PagerTabStrip实现页面滑动效果
- Android中使用ViewPager和PagerTabStrip实现页面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- android ViewPager 实现Gallery 效果和滑动特效
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android切换页面效果的实现二:WebView+ViewPager
- Android切换页面效果的实现二:WebView+ViewPager
- android自定义ViewPager之——水平滑动弹性效果
- 关于ORACLE COMMIT操作的详解
- action session,jsp session servlet session
- ZOJ2686 Cycle Game (博弈,找规律,搜索)
- 第四章 时钟 windows程序设计 王艳平版
- android.view.WindowLeaked 的解决方案
- Android: 扩展WebView 和ViewPager实现viewpager中的水平滑动,类似Gmail的效果
- 让我们如此享受的慢性毒药
- 纯CSS制作三角形的方法
- 心之殇
- 关于Excel vba 提示用户定义类型未定义
- JQuery easyUI DataGrid 创建复杂列表头
- 查看锁表进程SQL语句
- 动态规划经典教程
- wxPython的简单使用