Android判断Touch为滑动事件还是操作控件

因为在项目中要判断WebView是否处于滚动状态,但它不像ListView有onScrollStateChanged方法来监听,要实现就得手动监听它的Touch事件。

谈起Touch事件不得说提到 onInterceptTouchEvent,它是ViewGroup的一个扩展函数,要实现高级效果 必不可少的就需要重写 onInterceptTouchEvent、对所有的 MotionEvent 起到一个过滤的作用,最终的目的是防止一些事件传递到它包含的子控件中。

以ScrollView为例介绍滑动滚动条的判定。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */
    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        return true;
    }
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_MOVE: {
            /*
             * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
             * whether the user has moved far enough from his original down touch.
             */
            /*
            * Locally do absolute value. mLastMotionY is set to the y value
            * of the down event.
            */
            final int activePointerId = mActivePointerId;
            if (activePointerId == INVALID_POINTER) {
                // If we don't have a valid id, the touch down wasn't on content.
                break;
            }
            //通过activePointerId 的值来获得 当前 触摸点的索引
            final int pointerIndex = ev.findPointerIndex(activePointerId);
            //通过索引的值获得当前触摸的y的值
            final float y = ev.getY(pointerIndex);
            final int yDiff = (int) Math.abs(y - mLastMotionY);
            //根据绝对值和零界点来判断是单击还是滑动滚动条
            if (yDiff > mTouchSlop) {
                mIsBeingDragged = true;
                mLastMotionY = y;
                initVelocityTrackerIfNotExists();
                mVelocityTracker.addMovement(ev);
                if (mScrollStrictSpan == null) {
                    mScrollStrictSpan = StrictMode.enterCriticalSpan("ScrollView-scroll");
                }
            }
            break;
        }
        case MotionEvent.ACTION_DOWN: {
            final float y = ev.getY();
            if (!inChild((int) ev.getX(), (int) y)) {
                mIsBeingDragged = false;
                recycleVelocityTracker();
                break;
            }
            /*
             * Remember location of down touch.
             * ACTION_DOWN always refers to pointer index 0.
             */
              //记录 mLastMotionY  和 mActivePointerId
            //在 MotionEvent.ACTION_MOVE 状态时会用到
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            initOrResetVelocityTracker();
            mVelocityTracker.addMovement(ev);
            /*
            * If being flinged and user touches the screen, initiate drag;
            * otherwise don't.  mScroller.isFinished should be false when
            * being flinged.
            */
            mIsBeingDragged = !mScroller.isFinished();
            if (mIsBeingDragged && mScrollStrictSpan == null) {
                mScrollStrictSpan = StrictMode.enterCriticalSpan("ScrollView-scroll");
            }
            break;
        }
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            /* Release the drag */
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            recycleVelocityTracker();
            if (mScroller.springBack(mScrollX, mScrollY, 0, 0, 0, getScrollRange())) {
                invalidate();
            }
            break;
        case MotionEvent.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            break;
    }
    /*
    * The only time we want to intercept motion events is if we are in the
    * drag mode.
    */
    return mIsBeingDragged;
}

Android判断Touch为滑动事件还是操作控件的更多相关文章

  1. Android 自定义简易的方向盘操作控件

    最近在做一款交互性较为复杂的APP,需要开发一个方向操作控件.最终用自定义控件做了一个简单的版本. 这里我准备了两张素材图,作为方向盘被点击和没被点击的背景图.下面看看自定义的Wheel类 publi ...

  2. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

  3. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  4. winform 跨线程操作控件

    当进行winform的开发时,经常遇到用时比较久的操作,在传统的单线程程序中,用户必须等待这个耗时操作完成以后才能进行下一步的操作,这个时候,多线程编程就派上用场了,将这个耗时的操作放到一个新的子线程 ...

  5. ANDROID L——Material Design详解(UI控件)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  6. WinForm中跨线程操作控件

    在WinForm编程时会遇到通过后台线程操作界面的情况,直接在后台线程执行的方法中直接操作控件会报错,这时候就要使用跨线程方式间接操作控件.下面是两种实现方式.   1.采用定义delegate的方式 ...

  7. C# 跨线程操作控件(简洁)

                                              C# 跨线程操作控件 .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生.解决此问题的方法有两个: 第一 ...

  8. Winform跨窗体操作控件(使用委托)

    Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...

  9. Android 打造完美的侧滑菜单/侧滑View控件

    概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...

随机推荐

  1. [转] cordova-plugin-x-toast

    本文转自:https://www.npmjs.com/package/cordova-plugin-x-toast cordova plugin add https://github.com/Eddy ...

  2. Android工程师入门(二)——不忙不累怎么睡。。

    安卓开发迫在眉睫,这周入个门吧! Android工程师入门(二) 四.在界面中显示图片 ImageView 是显示图片的一个控件. --属性 src——内容图片: background——背景图片/背 ...

  3. 从WinCE到Linux

    到新的公司已经快两个月了,新的工作主要方向是Linux驱动移植和Android系统定制.由于项目还在立项的阶段,并没有分配具体的工作任务,所以找来一个Linux的开发板先玩一玩.它采用的处理器NUC9 ...

  4. 为什么那么多人想开发一元夺宝类app?

    别拿你的无知和愚蠢,来证明主观的判断! 国人对一切事物具有怀疑的本性是好的, 但是若不建立于科学的分析方法, 那就是愚昧! 身边有朋友玩夺宝投入较多,产出较少,于是向我求助.想从数据分析的角度知道到底 ...

  5. HTML 学习笔记 CSS3 (2D Matrix)

    Matrix 矩阵 那么什么是矩阵呢? 矩阵可以理解为方阵,只不过 平时方阵里面站着人 矩阵中是数值: CSS3中的矩阵: css3中的矩阵指的是一个方法,书写为matrix() 和 matrix3d ...

  6. mysql下mysqladmin日常管理命令总结

    mysqladmin 工具的使用格式:mysqladmin [option] command [command option] command ......参数选项:-c number 自动运行次数统 ...

  7. .NET Framework 中的所有类型

    .NET Framework 中的所有类型不是值类型就是引用类型. 值类型是使用对象实际值来表示对象的数据类型. 如果向一个变量分配值类型的实例,则该变量将被赋以该值的全新副本. 引用类型是使用对对象 ...

  8. vs2013怎么打开vs2010的解决方案

    1.直接用vs2013打开解决方案的sln文件,vs会自动进行转换的2.或者你用记事本的方式打开sln文件 将版本号改一下Microsoft Visual Studio Solution File, ...

  9. [Azure] 使用 Visual Studio 2013 管理中国版 Azure 订阅

    比较关心微软平台技术的朋友应该都知道,微软云服务(Microsoft Azure)以下简称Azure分为全球版和中国版,由于政府法规问题中国版的服务是由二十一世纪互联运营,整体来看中国版Azure和全 ...

  10. 傅盛:如何快慢“炼”金山?(转)

    原文地址:http://www.huxiu.com/article/16052/1.html 一直以来,金山都不是一家"大公司",从前不是,现在也不是. 能够掰着指头数完腾讯六大事 ...