Android中ScrollView嵌套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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值