主要通过重写 onInterceptTouchEvent 事件来解决,代码如下:

  1. package com.cm.android.pad.view.itemView;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.DisplayMetrics;
  5. import android.util.Log;
  6. import android.view.MotionEvent;
  7. import android.view.VelocityTracker;
  8. import android.view.ViewConfiguration;
  9. import android.view.WindowManager;
  10. import android.widget.ScrollView;
  11. import android.widget.Scroller;
  12. public class MultiScroll extends ScrollView {
  13. private static final int ANIMATION_SCREEN_SET_DURATION_MILLIS = 500;
  14. // What fraction (1/x) of the screen the user must swipe to indicate a page change
  15. private static final int FRACTION_OF_SCREEN_WIDTH_FOR_SWIPE = 4;
  16. private static final int INVALID_SCREEN = -1;
  17. /*
  18. * Velocity of a swipe (in density-independent pixels per second) to force a swipe to the
  19. * next/previous screen. Adjusted into mDensityAdjustedSnapVelocity on init.
  20. */
  21. private static final int SNAP_VELOCITY_DIP_PER_SECOND = 600;
  22. // Argument to getVelocity for units to give pixels per second (1 = pixels per millisecond).
  23. private static final int VELOCITY_UNIT_PIXELS_PER_SECOND = 1000;
  24. private static final int TOUCH_STATE_REST = 0;
  25. private static final int TOUCH_STATE_HORIZONTAL_SCROLLING = 1;
  26. private static final int TOUCH_STATE_VERTICAL_SCROLLING = -1;
  27. private int mCurrentScreen;
  28. private int mDensityAdjustedSnapVelocity;
  29. private boolean mFirstLayout = true;
  30. private float mLastMotionX;
  31. private float mLastMotionY;
  32. //private OnScreenSwitchListener mOnScreenSwitchListener;
  33. private int mMaximumVelocity;
  34. private int mNextScreen = INVALID_SCREEN;
  35. private Scroller mScroller;
  36. private int mTouchSlop;
  37. private int mTouchState = TOUCH_STATE_REST;
  38. private VelocityTracker mVelocityTracker;
  39. private int mLastSeenLayoutWidth = -1;
  40. public MultiScroll(Context context) {
  41. super(context);
  42. init();
  43. }
  44. public MultiScroll(Context context, AttributeSet attrs, int defStyle) {
  45. super(context, attrs, defStyle);
  46. init();
  47. }
  48. public MultiScroll(Context context, AttributeSet attrs) {
  49. super(context, attrs);
  50. init();
  51. }
  52. private void init() {
  53. mScroller = new Scroller(getContext());
  54. // Calculate the density-dependent snap velocity in pixels
  55. DisplayMetrics displayMetrics = new DisplayMetrics();
  56. ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
  57. .getMetrics(displayMetrics);
  58. mDensityAdjustedSnapVelocity =
  59. (int) (displayMetrics.density * SNAP_VELOCITY_DIP_PER_SECOND);
  60. final ViewConfiguration configuration = ViewConfiguration.get(getContext());
  61. mTouchSlop = configuration.getScaledTouchSlop();
  62. mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
  63. }
  64. @Override
  65. public boolean onInterceptTouchEvent(final MotionEvent ev) {
  66. /*
  67. * By Yoni Samlan: Modified onInterceptTouchEvent based on standard ScrollView's
  68. * onIntercept. The logic is designed to support a nested vertically scrolling view inside
  69. * this one; once a scroll registers for X-wise scrolling, handle it in this view and don't
  70. * let the children, but once a scroll registers for y-wise scrolling, let the children
  71. * handle it exclusively.
  72. */
  73. final int action = ev.getAction();
  74. boolean intercept = false;
  75. switch (action) {
  76. case MotionEvent.ACTION_MOVE:
  77. /*
  78. * If we're in a horizontal scroll event, take it (intercept further events). But if
  79. * we're mid-vertical-scroll, don't even try; let the children deal with it. If we
  80. * haven't found a scroll event yet, check for one.
  81. */
  82. if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) {
  83. // Let children handle the events for the duration of the scroll event.
  84. intercept = false;
  85. } else if (mTouchState == TOUCH_STATE_VERTICAL_SCROLLING) {
  86. /*
  87. * We've already started a horizontal scroll; set intercept to true so we can
  88. * take the remainder of all touch events in onTouchEvent.
  89. */
  90. intercept = true;
  91. } else { // We haven't picked up a scroll event yet; check for one.
  92. /*
  93. * If we detected a horizontal scroll event, start stealing touch events (mark
  94. * as scrolling). Otherwise, see if we had a vertical scroll event -- if so, let
  95. * the children handle it and don't look to intercept again until the motion is
  96. * done.
  97. */
  98. final float x = ev.getX();
  99. final int xDiff = (int) Math.abs(x - mLastMotionX);
  100. boolean xMoved = xDiff > mTouchSlop;
  101. final float y = ev.getY();
  102. final int yDiff = (int) Math.abs(y - mLastMotionY);
  103. boolean yMoved = yDiff > mTouchSlop;
  104. if (xMoved) {
  105. // Scroll if the user moved far enough along the X axis
  106. if(xDiff>=yDiff)
  107. mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING;
  108. mLastMotionX = x;
  109. }
  110. if (yMoved) {
  111. if(yDiff>xDiff)
  112. mTouchState = TOUCH_STATE_VERTICAL_SCROLLING;
  113. mLastMotionY = y;
  114. }
  115. }
  116. break;
  117. case MotionEvent.ACTION_CANCEL:
  118. case MotionEvent.ACTION_UP:
  119. // Release the drag.
  120. mTouchState = TOUCH_STATE_REST;
  121. intercept = false;
  122. break;
  123. case MotionEvent.ACTION_DOWN:
  124. /*
  125. * No motion yet, but register the coordinates so we can check for intercept at the
  126. * next MOVE event.
  127. */
  128. //Log.i("ViewPager-->", "Action_Down");
  129. mTouchState = TOUCH_STATE_REST;
  130. mLastMotionY = ev.getY();
  131. mLastMotionX = ev.getX();
  132. break;
  133. default:
  134. break;
  135. }
  136. Log.i("MultiScroll-->", intercept+"");
  137. return intercept;
  138. }
  139. }

Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)的更多相关文章

  1. Android开发之手势滑动(滑动手势监听)详解

    Android开发之手势滑动(滑动手势监听)详解 在Android应用中,经常需要手势滑动操作,比如上下滑动,或左右方向滑动,处理手势滑动通常有两种方法:一种是单独实现setOnTouchListen ...

  2. Android 自定义ScrollView的滑动监听事件

    项目结构: 1.LazyScrollView类(自定义ScrollView) package android.zhh.com.myapplicationscrollview; /** * Create ...

  3. Android开发之源码:多次点击事件的原理和实现

    多次点击事件 多次点击事件原理:最后一次点击事件与第一次点击事件的时间间隔是否小于某个时间,当小于的时候,就认为这是一个多次点击事件. Android源码实现效果: import android.ap ...

  4. android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法

    1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...

  5. JS 事件冒泡整理 浏览器的事件流

    JavaScript与HTML的交互通过事件来实现.而浏览器的事件流是一个非常重要的概念.不去讨论那些古老的浏览器有事件捕获与事件冒泡的争议, 只需要知道在DOM2中规定的事件流包括了三个部分,事件捕 ...

  6. Android开发(十九)——ViewFlipper中的onClick事件和onFling事件冲突

    在onDown中设置this.flipper.setClickable(true); 然后在onFling方法中this.flipper.setClickable(false); ps: 其中setO ...

  7. Android监测手指上下左右滑动屏幕

    在开发android程序时,有时会需要监测手指滑动屏幕,当手指朝上下左右不同方向滑动时做出不同的响应,那怎么去实现呢? 利用Android提供的手势监测器就可以很方便的实现,直接上代码(已测试通过) ...

  8. android开发学习 ------- 自定义View 圆 ,其点击事件 及 确定当前view的层级关系

    我需要实现下面的效果:   参考文章:https://blog.csdn.net/halaoda/article/details/78177069 涉及的View事件分发机制 https://www. ...

  9. Android开发--ScrollView的应用

    1.简介 当内容无法全部显示时,需要采取滚动的方式获取其与内容.其中,ScrollView为垂直滚动控件,HorizontalScrollView为水平滚动控件. 2.构建

随机推荐

  1. WIN10 搜索功能无法搜索本地应用

    原因是使用360卫士此类软件把windows search 服务给禁掉了. 解决方案很简单,就是把windows search 服务重新设置成自启动,并立刻启动,就ok了. 至于如何打开服务组件,可以 ...

  2. NandFlash

    一.概述 1.NandFlash NAND结构能提供极高的单元密度,可以达到高存储密度,比如能达到256M,并且写入和擦除的速度也很快.应用NAND的困难在于flash的管理需要特殊的系统接口. 2. ...

  3. php开发入门教程

    LAMP window:WAMP(windows,apache,mysql,php) LAMP是 Linux,Apache,MySQL和PHP的缩写,是我们提供 Web 服务的软件基础. 对于 Lin ...

  4. House of hello恶搞凯莉迷你包

    欧洲站 House of hello恶搞凯莉迷你包 最近淘宝卖的很疯,看看价格,俺咂舌不已 :1300-1600 今年迷你包卖的很疯我是知道的,迷你包今年没有买一个也是不行的! 剔除暴利,便宜的亲们不 ...

  5. Apache Maven 入门篇(下)

    第一篇文章大概的介绍了一下Apache Maven以及它的下载和安装,并且运行了一个简单的示例.那么在对maven有了一点接触后,接下去的一步是要了解maven的核心概念,这样才能在使用maven的时 ...

  6. [BZOJ 1033] [ZJOI2008] 杀蚂蚁antbuster 【模拟!】

    题目链接: BZOJ - 1033 题目分析 模拟!纯粹按照题目描述模拟! 这是一道喜闻乐见的经典模拟题! 我一共写了2遍,Debug 历时2天的所有晚自习 ... 时间超过 8h ... 我真是太弱 ...

  7. unity3d中的http通信 二

    转载自 http://www.cnblogs.com/88999660/archive/2013/03/11/2954279.html 如果侵权,请及时通知我删除! using System; usi ...

  8. keil uVision4一些使用总结(汉字注释,C关键字等)

    近日心血来潮,下载了最新的版的keil,再加上protues ,想弄个虚拟环境.主要原因还是经济问题.电子元件,是要花钱的... 今天遇到些keil uVision 4使用方面的问题,记录下来,方便以 ...

  9. Struts2 权限验证

    之前的Struts2项目通过再Sitemesh的母版页中使用Struts的if标签进行了session判断,使得未登录的用户不能看到页面,但是这 种现仅仅在view层进行,如果未登录用户直接在地址栏输 ...

  10. Microsoft Office 2013 激活方法

    Microsoft Office 2013 激活方法   Microsoft Office 2013是微软的新一代Office办公软件,全面采用Metro界面,包括Word.PowerPoint.Ex ...