Android实现双击事件的两种方式
生活随笔
收集整理的這篇文章主要介紹了
Android实现双击事件的两种方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Work around的方法是先監(jiān)聽onTouch事件來監(jiān)聽連續(xù)點擊次數(shù),每次點擊都布置一個間隔時間的延時任務,延時任務執(zhí)行時判斷間隔內是否還有點擊,如果沒有則發(fā)布點擊次數(shù),重置計數(shù)。
實現(xiàn)代碼如下:
/*** 連續(xù)點擊事件監(jiān)聽器 可以用作雙擊事件* */ public abstract class OnMultiTouchListener implements OnTouchListener {/*** 上次 onTouch 發(fā)生的時間*/private long lastTouchTime = 0;/*** 已經(jīng)連續(xù) touch 的次數(shù)*/private AtomicInteger touchCount = new AtomicInteger(0);private Runnable mRun = null;public void removeCallback() {if (mRun != null) {getMainLoopHandler().removeCallbacks(mRun);mRun = null;}}@Overridepublic 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() {@Overridepublic void run() {//兩個變量相等,表示時隔 multiTouchInterval之后沒有新的touch產(chǎn)生, 觸發(fā)事件并重置touchCountif (now == lastTouchTime) {onMultiTouch(v, event, touchCount.get());touchCount.set(0);}}};postTaskInUIThread(mRun, getMultiTouchInterval());}return true;}/*** 連續(xù)touch的最大間隔, 超過該間隔將視為一次新的touch開始, 默認是400,推薦值,也可以由客戶代碼指定* * @return*/protected int getMultiTouchInterval() {return 400;}/*** 連續(xù)點擊事件回調* * @param v* @param event* @param touchCount* 連續(xù)點擊的次數(shù)* @return*/public abstract void onMultiTouch(View v, MotionEvent event, int touchCount); }
使用代碼:
mView.setOnTouchListener( new OnMultiTouchListener() {@Overridepublic void onMultiTouch(View v, MotionEvent event, int touchCount) {if (touchCount == 2) {//雙擊}}});?
簡潔直接的方法是用GestureDector類:
mView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {GestureDetector detector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {@Overridepublic boolean onSingleTapUp(MotionEvent e) {return super.onSingleTapUp(e);}@Overridepublic void onLongPress(MotionEvent e) {super.onLongPress(e);}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return super.onScroll(e1, e2, distanceX, distanceY);}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {return super.onFling(e1, e2, velocityX, velocityY);}@Overridepublic void onShowPress(MotionEvent e) {super.onShowPress(e);}@Overridepublic boolean onDown(MotionEvent e) {return super.onDown(e);}@Overridepublic boolean onDoubleTap(MotionEvent e) {return super.onDoubleTap(e);}@Overridepublic boolean onDoubleTapEvent(MotionEvent e) {return super.onDoubleTapEvent(e);}@Overridepublic boolean onSingleTapConfirmed(MotionEvent e) {return super.onSingleTapConfirmed(e);}@Overridepublic boolean onContextClick(MotionEvent e) {return super.onContextClick(e);}});detector.onTouchEvent(event);return false;}});?
轉載于:https://www.cnblogs.com/mosthink/p/5524758.html
總結
以上是生活随笔為你收集整理的Android实现双击事件的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从网上搜索到的虚拟化笔记
- 下一篇: DTCC2016