首页 技术 正文
技术 2022年11月21日
0 收藏 981 点赞 3,370 浏览 4921 个字

主要通过重写 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. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856