转自:http://stormzhang.github.io/android/2013/07/27/android-scrollview-nested-webview/
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);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public 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.MyScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg"
android:layout_marginTop="@dimen/default_shadow_margin" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="180dp" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_top_show"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android: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>
<WebView
android: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>
相关推荐
Android仿小米商城商品详情界面UI,ScrollView嵌套ScrollView/WebView/ListView
Android仿小米商城商品详情界面UI,ScrollView嵌套ScrollView/WebView/ListView
解决scrollview中嵌套webview中包含iframe所导致的显示问题。
ScrollView嵌套RecycleView,会出现RecycleView显示不出来的问题,或者滑动不顺畅的问题,这里可以帮你解决。
但是android的webview默认支持的功能非常弱,很多地方都是需要自定义的,才能达到我们想要的效果。并且webview在不同的版本会有不同程度的bug。下面小编把webview经常出现的问题给大家整理如下: 1.为WebView自定义...
本篇文章主要介绍了Android Webview与ScrollView的滚动兼容及留白处理的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在做安卓商城项目时,产品经理老是拿京东和淘宝做样板,然后,就有了这个demo,实现在商品详情页上拉显示详情,主要技术就是处理ScrollView嵌套ScrollView或WebView时触摸、滑动事件的交互处理
本来感觉实现起来挺简单的,我打算的实现方式是使用scrollview嵌套recyclerview、webview、scrollview。但是一旦牵涉到滑动的嵌套就难免会发生嵌套之后滑动事件的冲突。这种实现的主要难点就是处理滑动事件的冲突...
RecyclerView滑动固定/悬停/折叠分组列表标题,NestScrollView折叠布局,解决和RecyclerView的滑动冲突, 自定义View(柱状直方图、金融收益图、无线轮播图、带Icon的TabView),WebView预加载,主流首页框架的多种...
ScrollView 嵌套 地址栏 和 WebView 手指滑屏向下滚动(网页向上),如果网页有滚动条,首先把 地址栏 滚动到消失,然后 WebView 才开始滚动; 手指滑屏向上滚动(网页向下),如果地址栏隐藏,那么 地址栏 首先慢慢...
比如ScrollView嵌套ListView,或者是ScrollView嵌套自己 这里先看一张效果图 上图是在购物软件上常见的上拉查看图文详情,关于这中动画效果的实现,其实实现整体的效果,办法是有很多的,网上有很多相关的例子,...
- ListView和ScrollView的嵌套冲突解决 - 监听软键盘回车按钮设置为搜索按钮 - 使用TextWatcher( )实时筛选 - 已搜索的关键字再次搜索不重复添加到数据库 - 刚进入页面设置软键盘不因为EditText而自动弹出
仿淘宝商品页上下滑动控件,这是一个viewGroup容器,实现上下两个frameLayout拖动切换,解决与WebView、ScrollView、ListView嵌套冲突。
苹果iOS是由苹果公司开发的手持设备操作系统。苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的,后来陆续套用到iPod touch、iPad以及Apple TV等苹果...Webview之UIWebView 离线浏览