2019独角兽企业重金招聘Python工程师标准>>>

Android中WebView用来加载html页面,自带滑动效果。ScrollView同样也是自带滑动效果,在项目中如果需要WebView和一些其他view比如TextView一起滑动的话就必须外面嵌套一层ScrollView,这时问题就来了,嵌套之后ScrollView的滑动和WebView的滑动就会有冲突,WebView的滑动不流畅。下面就是解决方案:

自定义一个ScrollView

public class MyScrollView extends ScrollView {private GestureDetector mGestureDetector;View.OnTouchListener mGestureListener;public MyScrollView(Context context, AttributeSet attrs) {super(context, attrs);mGestureDetector = new GestureDetector(context, new YScrollDetector());setFadingEdgeLength(0);}@Overridepublic boolean  {return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);}// Return false if we're scrolling in the x directionclass YScrollDetector extends SimpleOnGestureListener {@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {if (Math.abs(distanceY) > Math.abs(distanceX)) {return true;}return false;}}
}

上面代码中onInterceptTouchEvent方法是关键,重写这个方法使如果ScrollView有touch事件时不被拦截,这样只要ScrollView有touch事件优先处理,这样就保证了滑动的流畅。

之后就在自己的xml布局文件里用MyScrollView代替ScrollView来布局就ok了。如:

<com.boohee.widgets.MyScrollViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/main_bg"android:layout_marginTop="@dimen/default_shadow_margin" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="180dp" ><android.support.v4.view.ViewPagerandroid:id="@+id/vp_top_show"android:layout_width="fill_parent"android:layout_height="fill_parent" /><LinearLayoutandroid:id="@+id/dot_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:gravity="center_horizontal"android:orientation="horizontal"android:padding="10dp" ></LinearLayout></RelativeLayout><WebViewandroid:id="@+id/wv_show"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layerType="software"android:scrollbars="none" /></LinearLayout>
</com.boohee.widgets.MyScrollView>

转载于:https://my.oschina.net/gavinjin/blog/206506

Android中ScrollView嵌套WebView相关推荐

  1. android中ScrollView嵌套ListView或GridView显示位置问题

    Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...

  2. Android中scrollview与webview冲突事件

    原文链接:http://www.linuxidc.com/Linux/2014-03/98847.htm ----------------------------------------------- ...

  3. 在android中ScrollView嵌套ScrollView解决方案

    文章转载自:http://www.jb51.net/article/33054.htm 大家好,众所周知,android里两个相同方向的ScrollView是不能嵌套的,那要是有这样的需求怎么办,接下 ...

  4. ScrollView嵌套WebView遇到的坑

    最近遇到一个问题,在项目中ScrollView嵌套了WebView,点击WebView后会向上自动滑动,查看了原因发现是因为WebView优先获取了焦点,导致点击WebView引起的滑动,只需要在We ...

  5. Android中RecyclerView嵌套RecyclerView或嵌套ListView

    Android中RecyclerView嵌套RecyclerView或嵌套ListView

  6. Android 中 ScrollView 的基本使用

    Android 中 ScrollView 的基本使用 1. 垂直滚动: ScrollView 2. 水平滚动: HorizontalScrollView 子布局只能有一个. 1. 垂直滚动: Scro ...

  7. weex解决scrollView嵌套webview高度问题,安卓自定义weex组件webview,

    weex页面有scrollView嵌套web的页面,如果不给web指定高度就显示不出来.RN也有这个问题,查了下全部是通过原生开启一个不显示的webview提前加载一遍再将高度传给weex来解决,这种 ...

  8. android解决ScrollView嵌套ListView不能下拉刷新

    为了不误导新人,这篇帖子写的比较早了,这里2016年2月23日21:33:20更新的内容: 千万不要在实际开发中用scrollview嵌套listview\recylerview来处理滑动嵌套,   ...

  9. android 中ScrollView的使用

    android中布局一般都有两种方式,一种xml声明,另外一种则是程序声明: xml: <?xml version="1.0" encoding="utf-8&qu ...

最新文章

  1. 阿里云 Ubuntu PHP7 Nginx Mysql 开发环境搭建
  2. oracle错误:1067进程意外终止
  3. 7张图讲透Java垃圾回收算法!学妹直呼666!!!
  4. 黑鲨helo支持html吗,黑鲨游戏手机Helo综合评测 到底值不值得买
  5. 结对项目 浪曦计时器
  6. android报错自动重启,Android Studio常见报错及处理办法
  7. linux下免密认证登录失败原因总结
  8. YOLOv7全文翻译
  9. mac 管理 android手机助手,Mac 篇十二:Mac如何更高效的管理安卓手机和iPhone,老机器也可用哦...
  10. 怎样用Python的Numpy库求反正切
  11. 推荐一款好用的CopyTranslator 翻译工具
  12. 无权图的单源最短路径
  13. SSI服务端包含技术
  14. 南京大学java机试,2019南京大学计算机本科生开放日机试
  15. 同步传输与异步传输的区别
  16. 数据结构: 中序非递归遍历二叉树
  17. Flexe2.0 学习笔记一(利用PopUpManager来显示一个简单窗体)
  18. 用计算机打元宵节快乐,元宵节快乐祝福短语
  19. 优秀的 OKR 案例参考
  20. 数据可视化是怎样创造出来的

热门文章

  1. 集字卡v4.3.4微信公众号原版三种UI+关键字卡控制+支持强制关注
  2. MNews 1.9 – 干净的新闻自媒体博客wordpress主题
  3. SQL存储过程调试方法
  4. css 多栏文字流 css two columns text flow
  5. Adaptive Images : 为不同的屏幕尺寸提供不同的图片
  6. 如何使用Pinterest新推出的功能Rich Pin
  7. php mvc 通过 htaccess 隐藏 index.php 或 index.php?arg=
  8. Flex之嵌入外部资源
  9. flex states的用法
  10. centos6.8 安装python3.6