Android:view的双击监听

自定义OnMultiTouchListener抽象类实现OnTouchListener接口

import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import java.util.concurrent.atomic.AtomicInteger;

public abstract class OnMultiTouchListener implements View.OnTouchListener {

    /**
     * 手势监听的模式(down--->0    up--->1)
     */
    private int mActionMode;


    /**
     * 双击间隔 可设置
     */
    private long mIntervalTime = 200;


    /**
     * 上次 onTouch 发生的时间
     */
    private long lastTouchTime = 0;
    /**
     * 已经连续 touch 的次数
     */
    private AtomicInteger touchCount = new AtomicInteger(0);

    private Runnable mRun = null;
    private Handler mHandler = new Handler();

    pubilc void removeCallback() {
        if (mRun != null) {
            mHandler.removeCallbacksAndMessages(null);
            mRun = null;
        }
    }

    

    public OnMultiTouchListener(){

    }

    public OnMultiTouchListener(int actionMode){
        mActionMode = actionMode;
    }

    public OnMultiTouchListener(long intervalTime){
        mIntervalTime = intervalTime;
    }

    public OnMultiTouchListener(long intervalTime,int actionMode){
        mIntervalTime = intervalTime;
        mActionMode = actionMode;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == mActionMode){
            final long now = System.currentTimeMillis();
            lastTouchTime = now;

            touchCount.incrementAndGet();
            //每点击一次就移除上一次的延迟任务,重新布置一个延迟任务
            removeCallback();

            mRun = () -> {
                //两个变量相等,表示时隔 multiTouchInterval之后没有新的touch产生, 触发事件并重置touchCount
                if (now == lastTouchTime) {
                    onMultiTouch(v, event, touchCount.get());
                    if (touchCount.get() == 1){
                        onSingleClick(v,event);
                    } else if (touchCount.get() == 2){
                        onDoubleClick(v,event);
                    }
                    touchCount.set(0);
                }
            };

            mHandler.postDelayed(mRun, getMultiTouchInterval());
        }

        return true;
    }

    /**
     * 连续touch的最大间隔, 超过该间隔将视为一次新的touch开始, 默认是200
     *
     * @return
     */
    private long getMultiTouchInterval() {
        return mIntervalTime;
    }



    public abstract void onSingleClick(View view, MotionEvent event);
    public abstract void onDoubleClick(View view, MotionEvent event);
    public abstract void onMultiTouch(View view, MotionEvent event,int count);

}

自定义监听器提供四种构造方法

1:空参构造(默认MotionEvent为Down,即按下生效;默认双击间隔为200ms)

view.setOnTouchListener(new OnMultiTouchListener() {
    @Override
    public void onSingleClick(View view, MotionEvent event) {
        mTvShow.setText("单击");
    }

    @Override
    public void onDoubleClick(View view, MotionEvent event) {
        mTvShow.setText("双击");
    }

    @Override
    public void onMultiTouch(View view, MotionEvent event, int count) {

    }
});

2:int类型 (可设置MotionEvent,MotionEvent.DOWN(0)或者MotionEvent.UP(1) ;默认双击间隔为200ms)

...

3:long类型(可设置双击间隔时长; 默认MotionEvent.DOWN(0))

...

4:双参构造(可设置MotionEvent(int类型)和间隔时长(long类型))

...

参考别人的博客做出一些修改,感谢!

原文链接:Android实现双击事件的两种方式 - Mosthink - 博客园

你可能感兴趣的:(Android,android,kotlin,flutter)