zoukankan      html  css  js  c++  java
  • Android实现双击事件的两种方式

    Work around的方法是先监听onTouch事件来监听连续点击次数,每次点击都布置一个间隔时间的延时任务,延时任务执行时判断间隔内是否还有点击,如果没有则发布点击次数,重置计数。

    实现代码如下:

    /**
     * 连续点击事件监听器 可以用作双击事件
     * 
     */
    public abstract class OnMultiTouchListener implements OnTouchListener {
        /**
         * 上次 onTouch 发生的时间
         */
        private long lastTouchTime = 0;
        /**
         * 已经连续 touch 的次数
         */
        private AtomicInteger touchCount = new AtomicInteger(0);
    
        private Runnable mRun = null;
    
        public void removeCallback() {
            if (mRun != null) {
           getMainLoopHandler().removeCallbacks(mRun);
                mRun = null;
            }
        }
    
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                final long now = System.currentTimeMillis();
                lastTouchTime = now;
    
                touchCount.incrementAndGet();
           //每点击一次就移除上一次的延迟任务,重新布置一个延迟任务
                removeCallback();
    
                mRun = new Runnable() {
                    @Override
                    public void run() {
                //两个变量相等,表示时隔 multiTouchInterval之后没有新的touch产生, 触发事件并重置touchCount
    if (now == lastTouchTime) { onMultiTouch(v, event, touchCount.get()); touchCount.set(0); } } }; postTaskInUIThread(mRun, getMultiTouchInterval()); } return true; } /** * 连续touch的最大间隔, 超过该间隔将视为一次新的touch开始, 默认是400,推荐值,也可以由客户代码指定 * * @return */ protected int getMultiTouchInterval() { return 400; } /** * 连续点击事件回调 * * @param v * @param event * @param touchCount * 连续点击的次数 * @return */ public abstract void onMultiTouch(View v, MotionEvent event, int touchCount); }

    使用代码:

    mView.setOnTouchListener( new OnMultiTouchListener() {
            @Override
            public void onMultiTouch(View v, MotionEvent event, int touchCount) {
                if (touchCount == 2) {
                  //双击
                }
            }
        });

    简洁直接的方法是用GestureDector类:

    mView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    GestureDetector detector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
    
                        @Override
                        public boolean onSingleTapUp(MotionEvent e) {
                            return super.onSingleTapUp(e);
                        }
    
                        @Override
                        public void onLongPress(MotionEvent e) {
                            super.onLongPress(e);
                        }
    
                        @Override
                        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                            return super.onScroll(e1, e2, distanceX, distanceY);
                        }
    
                        @Override
                        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                            return super.onFling(e1, e2, velocityX, velocityY);
                        }
    
                        @Override
                        public void onShowPress(MotionEvent e) {
                            super.onShowPress(e);
                        }
    
                        @Override
                        public boolean onDown(MotionEvent e) {
                            return super.onDown(e);
                        }
    
                        @Override
                        public boolean onDoubleTap(MotionEvent e) {
                            return super.onDoubleTap(e);
                        }
    
                        @Override
                        public boolean onDoubleTapEvent(MotionEvent e) {
                            return super.onDoubleTapEvent(e);
                        }
    
                        @Override
                        public boolean onSingleTapConfirmed(MotionEvent e) {
                            return super.onSingleTapConfirmed(e);
                        }
    
                        @Override
                        public boolean onContextClick(MotionEvent e) {
                            return super.onContextClick(e);
                        }
                    });
                    detector.onTouchEvent(event);
    
                    return false;
                }
            });
  • 相关阅读:
    加密模块
    Flask_Blueprint(蓝图)
    Python中__get__ ,__getattr__ ,__getattribute__用法与区别?
    为什么要使用数据库连接池?以及用法(DBUtils)
    Flask_配置文件
    CRM知识点汇总(未完💩💩💩💩💩)
    popUp
    Django_调查问卷
    Django_form
    Numpy
  • 原文地址:https://www.cnblogs.com/mosthink/p/5524758.html
Copyright © 2011-2022 走看看