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

   需求:

              最近在做一个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事件并没有什么直接关系,很费解,以后有时间再详细研究一下。

       


 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值