Android Webview与ScrollView事件分发

版权声明:本文为博主原创文章,未经博主允许不得转载: https://blog.csdn.net/sinat_33381791/article/details/82746930

简介

在开发中我们经常遇到各种各样的与ScrollView的事件分发,如 ListView 、RecyclView等,那么今天我们就来说说WebView与ScrollView之间的事件分发,为什么会有这样的功能需要我们处理呢?因为WebView当页面铺满整个屏幕时,触摸向上向下是会自动的进行滚动的效果,而ScrollView是当各个控件如果多的时候,页面铺满了,为了能让一个页面能显示我们想要达到的效果而引用它,因为它具备自动滚动的效果。
所以,WebView与ScrollView要进行事件分发,才能达到我们想要的效果。

代码如下

1、其实webView与ScrollView之间的事件分发与ListView、RecyleView没有多大的区别,这里讲一种,首先自定义一个MyScrollView去继承ScrollView,代码如下:

public class MyScrollView extends ScrollView {
  private GestureDetector mGesture;

  public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mGesture= new GestureDetector(context, new YScrollDetector());
    setFadingEdgeLength(0);
  }

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return super.onInterceptTouchEvent(ev) &&    
    mGesture.onTouchEvent(ev);
}
//当向上或者向下活动的时候进行分发
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;
    }
}}

2、在布局文件中使用如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    <RelativeLayout
      android:id="@+id/title_bar"
      android:layout_width="match_parent"
      android:layout_height="@dimen/hx_head_height"
      android:background="@color/white"
      android:gravity="center_vertical">
      <TextView
        android:id="@+id/main_btc_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/text_size_three"
        android:background="@android:color/transparent"
        android:text="登录"
        android:textSize="18sp"/>

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/home"
        android:textSize="20sp"
        android:visibility="visible"/>

    <ImageButton
        android:id="@+id/main_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/text_size_three"
        android:background="@drawable/bg_menu_user_click"
        android:src="@mipmap/btn_mine_set02_normal"/>

</RelativeLayout>
  <!-- 引用自定义ScrollView-->
<com.example.apple.hxbtc.view.MyScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
        <com.youth.banner.Banner
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            app:indicator_height="8dp"
            app:indicator_width="8dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_below="@id/aboutus_hotline"
            android:background="@color/custom_division_line_color"/>
        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layerType="software"
            android:scrollbars="none">
        </WebView>
    </LinearLayout>
</com.example.apple.hxbtc.view.MyScrollView>
</LinearLayout>

3、效果如图所示:

  • 一、滑动前


S70227-165833.jpg

  • 二、滑动后


S70227-165839.jpg

总结

以前开发的小例子,将笔记整理一下贴了出来,希望对大家有一些帮助,欢迎大家一起学习Android开发。

推荐

欢迎大家浏览我的博客http://eirunye.github.io

猜你喜欢

转载自blog.csdn.net/sinat_33381791/article/details/82746930