Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)
主要通过重写 onInterceptTouchEvent 事件来解决,代码如下:
- package com.cm.android.pad.view.itemView;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
- import android.view.ViewConfiguration;
- import android.view.WindowManager;
- import android.widget.ScrollView;
- import android.widget.Scroller;
- public class MultiScroll extends ScrollView {
- private static final int ANIMATION_SCREEN_SET_DURATION_MILLIS = 500;
- // What fraction (1/x) of the screen the user must swipe to indicate a page change
- private static final int FRACTION_OF_SCREEN_WIDTH_FOR_SWIPE = 4;
- private static final int INVALID_SCREEN = -1;
- /*
- * Velocity of a swipe (in density-independent pixels per second) to force a swipe to the
- * next/previous screen. Adjusted into mDensityAdjustedSnapVelocity on init.
- */
- private static final int SNAP_VELOCITY_DIP_PER_SECOND = 600;
- // Argument to getVelocity for units to give pixels per second (1 = pixels per millisecond).
- private static final int VELOCITY_UNIT_PIXELS_PER_SECOND = 1000;
- private static final int TOUCH_STATE_REST = 0;
- private static final int TOUCH_STATE_HORIZONTAL_SCROLLING = 1;
- private static final int TOUCH_STATE_VERTICAL_SCROLLING = -1;
- private int mCurrentScreen;
- private int mDensityAdjustedSnapVelocity;
- private boolean mFirstLayout = true;
- private float mLastMotionX;
- private float mLastMotionY;
- //private OnScreenSwitchListener mOnScreenSwitchListener;
- private int mMaximumVelocity;
- private int mNextScreen = INVALID_SCREEN;
- private Scroller mScroller;
- private int mTouchSlop;
- private int mTouchState = TOUCH_STATE_REST;
- private VelocityTracker mVelocityTracker;
- private int mLastSeenLayoutWidth = -1;
- public MultiScroll(Context context) {
- super(context);
- init();
- }
- public MultiScroll(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
- public MultiScroll(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- private void init() {
- mScroller = new Scroller(getContext());
- // Calculate the density-dependent snap velocity in pixels
- DisplayMetrics displayMetrics = new DisplayMetrics();
- ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
- .getMetrics(displayMetrics);
- mDensityAdjustedSnapVelocity =
- (int) (displayMetrics.density * SNAP_VELOCITY_DIP_PER_SECOND);
- final ViewConfiguration configuration = ViewConfiguration.get(getContext());
- mTouchSlop = configuration.getScaledTouchSlop();
- mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
- }
- @Override
- public boolean onInterceptTouchEvent(final MotionEvent ev) {
- /*
- * By Yoni Samlan: Modified onInterceptTouchEvent based on standard ScrollView's
- * onIntercept. The logic is designed to support a nested vertically scrolling view inside
- * this one; once a scroll registers for X-wise scrolling, handle it in this view and don't
- * let the children, but once a scroll registers for y-wise scrolling, let the children
- * handle it exclusively.
- */
- final int action = ev.getAction();
- boolean intercept = false;
- switch (action) {
- case MotionEvent.ACTION_MOVE:
- /*
- * If we're in a horizontal scroll event, take it (intercept further events). But if
- * we're mid-vertical-scroll, don't even try; let the children deal with it. If we
- * haven't found a scroll event yet, check for one.
- */
- if (mTouchState == TOUCH_STATE_HORIZONTAL_SCROLLING) {
- // Let children handle the events for the duration of the scroll event.
- intercept = false;
- } else if (mTouchState == TOUCH_STATE_VERTICAL_SCROLLING) {
- /*
- * We've already started a horizontal scroll; set intercept to true so we can
- * take the remainder of all touch events in onTouchEvent.
- */
- intercept = true;
- } else { // We haven't picked up a scroll event yet; check for one.
- /*
- * If we detected a horizontal scroll event, start stealing touch events (mark
- * as scrolling). Otherwise, see if we had a vertical scroll event -- if so, let
- * the children handle it and don't look to intercept again until the motion is
- * done.
- */
- final float x = ev.getX();
- final int xDiff = (int) Math.abs(x - mLastMotionX);
- boolean xMoved = xDiff > mTouchSlop;
- final float y = ev.getY();
- final int yDiff = (int) Math.abs(y - mLastMotionY);
- boolean yMoved = yDiff > mTouchSlop;
- if (xMoved) {
- // Scroll if the user moved far enough along the X axis
- if(xDiff>=yDiff)
- mTouchState = TOUCH_STATE_HORIZONTAL_SCROLLING;
- mLastMotionX = x;
- }
- if (yMoved) {
- if(yDiff>xDiff)
- mTouchState = TOUCH_STATE_VERTICAL_SCROLLING;
- mLastMotionY = y;
- }
- }
- break;
- case MotionEvent.ACTION_CANCEL:
- case MotionEvent.ACTION_UP:
- // Release the drag.
- mTouchState = TOUCH_STATE_REST;
- intercept = false;
- break;
- case MotionEvent.ACTION_DOWN:
- /*
- * No motion yet, but register the coordinates so we can check for intercept at the
- * next MOVE event.
- */
- //Log.i("ViewPager-->", "Action_Down");
- mTouchState = TOUCH_STATE_REST;
- mLastMotionY = ev.getY();
- mLastMotionX = ev.getX();
- break;
- default:
- break;
- }
- Log.i("MultiScroll-->", intercept+"");
- return intercept;
- }
- }
Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)的更多相关文章
- Android开发之手势滑动(滑动手势监听)详解
Android开发之手势滑动(滑动手势监听)详解 在Android应用中,经常需要手势滑动操作,比如上下滑动,或左右方向滑动,处理手势滑动通常有两种方法:一种是单独实现setOnTouchListen ...
- Android 自定义ScrollView的滑动监听事件
项目结构: 1.LazyScrollView类(自定义ScrollView) package android.zhh.com.myapplicationscrollview; /** * Create ...
- Android开发之源码:多次点击事件的原理和实现
多次点击事件 多次点击事件原理:最后一次点击事件与第一次点击事件的时间间隔是否小于某个时间,当小于的时候,就认为这是一个多次点击事件. Android源码实现效果: import android.ap ...
- android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法
1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...
- JS 事件冒泡整理 浏览器的事件流
JavaScript与HTML的交互通过事件来实现.而浏览器的事件流是一个非常重要的概念.不去讨论那些古老的浏览器有事件捕获与事件冒泡的争议, 只需要知道在DOM2中规定的事件流包括了三个部分,事件捕 ...
- Android开发(十九)——ViewFlipper中的onClick事件和onFling事件冲突
在onDown中设置this.flipper.setClickable(true); 然后在onFling方法中this.flipper.setClickable(false); ps: 其中setO ...
- Android监测手指上下左右滑动屏幕
在开发android程序时,有时会需要监测手指滑动屏幕,当手指朝上下左右不同方向滑动时做出不同的响应,那怎么去实现呢? 利用Android提供的手势监测器就可以很方便的实现,直接上代码(已测试通过) ...
- android开发学习 ------- 自定义View 圆 ,其点击事件 及 确定当前view的层级关系
我需要实现下面的效果: 参考文章:https://blog.csdn.net/halaoda/article/details/78177069 涉及的View事件分发机制 https://www. ...
- Android开发--ScrollView的应用
1.简介 当内容无法全部显示时,需要采取滚动的方式获取其与内容.其中,ScrollView为垂直滚动控件,HorizontalScrollView为水平滚动控件. 2.构建
随机推荐
- Linux之Vim编辑器使用
vim文本编辑器用于建立 编辑 显示文本文件,vim没有菜单,只有命令 在windows 平台下可使用gvim进行编写 Vim三种工作模式: 常有命令: 1.INSERT插入命令 i 在光标前插入 I ...
- Tomcat虚拟主机配置
3.1.配置虚拟主机 配置虚似主机就是配置一个网站. 在Tomcat服务器配置一个虚拟主机(网站),需要修改conf文件夹下的server.xml这个配置文件,使用Host元素进行配置,打开serve ...
- 基于php-fpm的配置详解[转载]
php自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pid,建 ...
- CSS远程加载字体
CSS 远程加载字体的方法,做网站CSS的都知道,用户浏览网站时,网页上的字体是加载本地的.换言之,如果网站使用了用户电脑所没有安装的字体,那显示字体就会被默认字体所代替了,自然效果就大受影响了. 上 ...
- vm拷贝cloudera-scm-agent造成问题
------------网络问题---------- ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting ...
- [BZOJ 1072] [SCOI2007] 排列perm 【状压DP】
题目链接:BZOJ 1072 这道题使用 C++ STL 的 next_permutation() 函数直接暴力就可以AC .(使用 Set 判断是否重复) 代码如下: #include <io ...
- A Statistical View of Deep Learning (II): Auto-encoders and Free Energy
A Statistical View of Deep Learning (II): Auto-encoders and Free Energy With the success of discrimi ...
- QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)
注意:本次实现线程的暂停执行主要采用互斥量的方法,如果有更好的实现方法的小伙伴可以在下面留言! 直接插入代码了,由于做的小demo,代码写的可能有点乱,但还算完整. 1 2 3 4 5 6 7 8 9 ...
- sphinx插入css
使用role指令达到目的. We can put following lines at the beginning of our RST file to specify its style. .. r ...
- 【HDOJ】2451 Simple Addition Expression
递推,但是要注意细节.题目的意思,就是求s(x) = i+(i+1)+(i+2),i<n.该表达中计算过程中CA恒为0(包括中间值)的情况.根据所求可推得.1-10: 31-100: 3*41- ...