您的位置:首页 > 移动开发

android控件-ScrollView 和WebView之见滑动冲突解决

2016-05-21 09:29 435 查看
需求:最近在做一个webView加载网页的页面,最外层是一个scrollView,因为还有标题等其他数据是需要单独获取加载,所以scrollview中是包含一个其他信息的头部布局和一个加载网页信息的WebView,当滑动的时候,头部和WebView一起滑动。出现bug只有WebView可以滑动。bug解决思路重写父控件scrollView,拦截WebView的onTouch事件自定义scrollview代码定义:
<span style="font-size:18px;">package dianshi.matchtrader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
* Created by Administrator on 2016/5/20 0020.
*/
public class MyScrollView extends ScrollView{

private GestureDetector mGestureDetector;

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
//初始化手势
mGestureDetector = new GestureDetector(context, new YScrollDetector());
}

/**
* touch事件的拦截函数
* @param ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//根据手势决定是否拦截子控件的onTouch事件
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}

/**
* 控件的手势监听
*/
class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//当纵向滑动的距离大于横向滑动的距离的时候,返回true
if (Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
}</span>
布局中使用:
<span style="font-size:18px;">    <dianshi.matchtrader.view.MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/txt_noticeDetail_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="22sp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/txt_noticeDetail_dayTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#a3a3a3"
android:textSize="16sp"/>
<TextView
android:id="@+id/txt_noticeDetail_hourTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/lightGray"
android:textSize="16sp"
android:layout_marginLeft="20dp"
/>

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="5dp">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_noticeDetail_content"
android:layout_margin="0dp"
android:padding="0dp"
android:layout_gravity="bottom"
/>

</LinearLayout>

</LinearLayout>

</dianshi.matchtrader.view.MyScrollView></span>
注意事项:使用的时候 scrollview不要添加android:fillViewport="true"属性,否则没有效果,原因暂时还不知道具体是为什么。百度到的android:fillViewport="true"的作用是:当ScrollView没有fillViewport=“true”时, 里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了"fill_parent"),而如果LinearLayout的元素设置了fill_parent,那么也是不管用的,因为LinearLayout依赖里面的元素,而里面的元素又依赖LinearLayout,这样自相矛盾.所以里面元素设置了fill_parent,也会当做wrap_content来计算。但是好像和touch事件并没有什么直接关系,很费解,以后有时间再详细研究一下。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: