Created
September 19, 2014 04:18
-
-
Save anagri/48c7bc6efb36e9eb40cc to your computer and use it in GitHub Desktop.
Android Continuous Long Click Listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.creativei.listener; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.view.HapticFeedbackConstants; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.Button; | |
public class ContinuousLongClickListener implements View.OnTouchListener, View.OnLongClickListener { | |
public static final int WHAT = 264776257; | |
private final View.OnLongClickListener onLongClickListener; | |
private final Handler handler; | |
private int delay; | |
public ContinuousLongClickListener(View view, View.OnLongClickListener onLongClickListener) { | |
this.onLongClickListener = onLongClickListener; | |
delay = 750; | |
handler = new Handler(); | |
view.setOnLongClickListener(this); | |
view.setOnTouchListener(this); | |
} | |
@Override | |
public boolean onLongClick(final View view) { | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); | |
onLongClickListener.onLongClick(view); | |
Message message = Message.obtain(handler, this); | |
message.what = WHAT; | |
handler.sendMessageDelayed(message, delay); | |
} | |
}); | |
return true; | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
handler.removeMessages(WHAT); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its fine... but not working within ListView/Recycler View.