[Android] TouchListener wich handle continuous press

Geoffrey Métais git at videolan.org
Thu Apr 30 17:02:29 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Apr 30 16:58:30 2015 +0200| [10141701a6b11360d744da4a01dd6003c279dd4a] | committer: Geoffrey Métais

TouchListener wich handle continuous press

Long press repeatedly triggers an OnClickListener.
This class implements OnTouchListener, so it can be used on every View type.

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=10141701a6b11360d744da4a01dd6003c279dd4a
---

 .../org/videolan/vlc/widget/OnRepeatListener.java  |   97 ++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/widget/OnRepeatListener.java b/vlc-android/src/org/videolan/vlc/widget/OnRepeatListener.java
new file mode 100644
index 0000000..474291a
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/widget/OnRepeatListener.java
@@ -0,0 +1,97 @@
+/*
+ * *************************************************************************
+ *  OnRepeatListener.java
+ * **************************************************************************
+ *  Copyright © 2015 VLC authors and VideoLAN
+ *  Author: Geoffrey Métais
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *  ***************************************************************************
+ */
+
+package org.videolan.vlc.widget;
+
+import android.os.Handler;
+import android.os.Message;
+import android.view.MotionEvent;
+import android.view.View;
+
+public class OnRepeatListener implements View.OnTouchListener {
+
+    private static final int ACTION_ONCLICK = 0;
+
+    //Default values in milliseconds
+    private static final int DEFAULT_INITIAL_DELAY = 1000;
+    private static final int DEFAULT_NORMAL_DELAY = 300;
+
+    private int mInitialInterval;
+    private final int mNormalInterval;
+    private final View.OnClickListener mClickListener;
+    private View downView;
+
+    /**
+     *
+     * @param initialInterval Initial interval in millis
+     * @param normalInterval Normal interval in millis
+     * @param clickListener The OnClickListener to trigger
+     */
+    public OnRepeatListener(int initialInterval, int normalInterval,
+                            View.OnClickListener clickListener) {
+        if (clickListener == null)
+            throw new IllegalArgumentException("null runnable");
+        if (initialInterval < 0 || normalInterval < 0)
+            throw new IllegalArgumentException("negative interval");
+
+        this.mInitialInterval = initialInterval;
+        this.mNormalInterval = normalInterval;
+        this.mClickListener = clickListener;
+    }
+
+    /**
+     *
+     * @param clickListener The OnClickListener to trigger
+     */
+    public OnRepeatListener(View.OnClickListener clickListener) {
+        this(DEFAULT_INITIAL_DELAY, DEFAULT_NORMAL_DELAY, clickListener);
+    }
+
+    public boolean onTouch(View view, MotionEvent motionEvent) {
+        switch (motionEvent.getAction()) {
+            case MotionEvent.ACTION_DOWN:
+                mHandler.removeMessages(ACTION_ONCLICK);
+                mHandler.sendEmptyMessageDelayed(ACTION_ONCLICK, mInitialInterval);
+                downView = view;
+                mClickListener.onClick(view);
+                return true;
+            case MotionEvent.ACTION_UP:
+            case MotionEvent.ACTION_CANCEL:
+                mHandler.removeMessages(ACTION_ONCLICK);
+                downView = null;
+                return true;
+        }
+        return false;
+    }
+
+    private Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what){
+                case ACTION_ONCLICK:
+                    sendEmptyMessageDelayed(ACTION_ONCLICK, mNormalInterval);
+                    mClickListener.onClick(downView);
+            }
+        }
+    };
+}



More information about the Android mailing list