Android ScrollView嵌套WebView
2015-12-29 15:38
501 查看
Android中WebView用来加载html页面,自带滑动效果。ScrollView同样也是自带滑动效果,在项目中如果需要WebView和一些其他view比如TextView一起滑动的话就必须外面嵌套一层ScrollView,这时问题就来了,嵌套之后ScrollView的滑动和WebView的滑动就会有冲突,WebView的滑动不流畅。下面就是解决方案:
第一种方法:我们都知道ScrollView和WebView都有滚动的效果,所以我们需要先屏蔽WebView的滚动事件。
这时候如果后端如果传过来的不是完整的Html,而是只有body部分的内容,那么我们就需要补充并添加一些css样式来达到自适应的效果。
4.0以后引用:
第二种方法:自定义一个ScrollView
之后就在自己的xml布局文件里用MyScrollView代替ScrollView来布局就ok了。如:
webview常见问题汇总/article/1875765.html
第一种方法:我们都知道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); } }
之后就在自己的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>注意:使用后发现webView加上
android:layerType="software"之后,有时候加载不出来数据,而且加载数据很慢。所以可以去掉。感谢点击打开链接
webview常见问题汇总/article/1875765.html
相关文章推荐
- Android中ProgressDialog的应用
- Android ListView性能优化实战方案
- Android完美解决输入框EditText隐藏密码打勾显示密码问题
- android常用广播
- ObjectAnimator设置动画
- IOS 9人机界面指南(1)
- IOS开发 CABasicAnimation
- android 沉浸通知栏
- android 创建多层树型结构
- Web移动端Fixed布局的解决方案
- android的5种数据存储方式
- iOS 开发搜索框 - 简单实现
- 限免应用太多容量太少?限时应用免费收纳术!
- Nagios全方位接入告警-微信/电话/短信都支持
- iOS时间与时间戳相互转换
- 利用Gradle实现android的版本共存
- android:clipToPadding和android:clipChildren
- Objective-C对方法进行注释的方法
- android 修改listview item view 的方法
- 一步一步教你做ios推送