Android ScrollView嵌套WebView
来源:互联网 发布:唇线的画法知乎 编辑:程序博客网 时间:2024/05/22 07:45
Android中WebView用来加载html页面,自带滑动效果。ScrollView同样也是自带滑动效果,在项目中如果需要WebView和一些其他view比如TextView一起滑动的话就必须外面嵌套一层ScrollView,这时问题就来了,嵌套之后ScrollView的滑动和WebView的滑动就会有冲突,WebView的滑动不流畅。下面就是解决方案:
第一种方法:我们都知道ScrollView和WebView都有滚动的效果,所以我们需要先屏蔽WebView的滚动事件。<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" >
这时候如果后端如果传过来的不是完整的Html,而是只有body部分的内容,那么我们就需要补充并添加一些css样式来达到自适应的效果。
private String getHtmlData(String bodyHTML) { String head = "<head>" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " + "<style>img{max-width: 100%; width:auto; height:auto;}</style>" + "</head>"; return "<html>" + head + "<body>" + bodyHTML + "</body></html>";}
4.0以后引用:
Webview.loadDataWithBaseURL("example-app://example.co.uk/", getHtmlData(String bodyHTML), null, "UTF-8",null);
第二种方法:自定义一个ScrollView
import android.content.Context;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.widget.ScrollView;/** * Created by Administrator on 2015/12/29 0029. */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 GestureDetector.SimpleOnGestureListener {// @Override// public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// if (Math.abs(distanceY) > Math.abs(distanceX)) {// return true;// }// return false;// }// } /** */ public ScrollView parentScrollView; public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } private int lastScrollDelta = 0; public void resume() { overScrollBy(0, -lastScrollDelta, 0, getScrollY(), 0, getScrollRange(), 0, 0, true); lastScrollDelta = 0; } int mTop = 10; /** * 将targetView滚到最顶端 */ public void scrollTo(View targetView) { int oldScrollY = getScrollY(); int top = targetView.getTop() - mTop; int delatY = top - oldScrollY; lastScrollDelta = delatY; overScrollBy(0, delatY, 0, getScrollY(), 0, getScrollRange(), 0, 0, true); } private int getScrollRange() { int scrollRange = 0; if (getChildCount() > 0) { View child = getChildAt(0); scrollRange = Math.max(0, child.getHeight() - (getHeight())); } return scrollRange; } int currentY; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (parentScrollView == null) { return super.onInterceptTouchEvent(ev); } else { if (ev.getAction() == MotionEvent.ACTION_DOWN) {// 将父scrollview的滚动事件拦截 currentY = (int)ev.getY(); setParentScrollAble(false); return super.onInterceptTouchEvent(ev); } else if (ev.getAction() == MotionEvent.ACTION_UP) {// 把滚动事件恢复给父Scrollview setParentScrollAble(true); } else if (ev.getAction() == MotionEvent.ACTION_MOVE) { } } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { View child = getChildAt(0); if (parentScrollView != null) { if (ev.getAction() == MotionEvent.ACTION_MOVE) { int height = child.getMeasuredHeight(); height = height - getMeasuredHeight();// System.out.println("height=" + height); int scrollY = getScrollY();// System.out.println("scrollY" + scrollY); int y = (int)ev.getY();// 手指向下滑动 if (currentY < y) { if (scrollY <= 0) {// 如果向下滑动到头,就把滚动交给父Scrollview setParentScrollAble(true); return false; } else { setParentScrollAble(false); } } else if (currentY > y) { if (scrollY >= height) {// 如果向上滑动到头,就把滚动交给父Scrollview setParentScrollAble(true); return false; } else { setParentScrollAble(false); } } currentY = y; } } return super.onTouchEvent(ev); } /** * 是否把滚动事件交给父scrollview * * @param flag */ private void setParentScrollAble(boolean flag) { parentScrollView.requestDisallowInterceptTouchEvent(!flag); }}
<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>注意:使用后发现webView加上
android:layerType="software"之后,有时候加载不出来数据,而且加载数据很慢。所以可以去掉。感谢点击打开链接
webview常见问题汇总http://blog.csdn.net/t12x3456/article/details/13769731
0 0
- Android ScrollView嵌套WebView
- android scrollview 嵌套 webview
- Android中ScrollView嵌套WebView
- 关于Android ScrollView嵌套WebView冲突问题
- android scrollview嵌套webview滑动冲突问题
- Android中scrollview嵌套webview的实现
- ScrollView嵌套webview
- scrollview嵌套webview冲突
- scrollview嵌套webview
- Android 开发中scrollview嵌套webview滑动冲突的解决方案
- Android scrollview嵌套webview滑动冲突的解决方案
- android - Webview嵌套于ScrollView页面有空白
- Android ViewPager、ScrollView或ListView嵌套WebView滑动冲突
- android scrollview嵌套webview横向滚动冲突的问题
- Android 开发中scrollview嵌套webview滑动冲突的解决方案
- Android-scrollview嵌套listview、webview、gridview不置顶问题
- ScrollView嵌套WebView冲突解决方法
- Android仿小米商城商品详情界面UI,ScrollView嵌套ScrollView/WebView/ListView
- 理解RESTFul架构
- h264 nal 结构总结
- 百度地图API 批量坐标转换
- ios React Native 入门环境配置
- Rsyslog配置文件详解
- Android ScrollView嵌套WebView
- iOS - 收到远程推送后的页面跳转
- android通过蓝牙连接打印机实现格式化打印(二)
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 二,图像遮盖,Canvas静态变化)
- synergy软件ubuntu配置
- web学习之入侵前的号角
- 关于img不含id,它的外部DIV层含有,对img的获取
- jQuery源码分析7--分离构造器
- NSNull报错