public static void setLongClickDelay(final Handler handler, final View v, long delay) {
final long delayMillis = delay < 500 ? 500 : delay;
v.setOnTouchListener(new View.OnTouchListener() {
private static final int TOUCH_MAX = 50;
private int mLastMotionX;
private int mLastMotionY;
private boolean mHasPerformedLongPress = false;
private Runnable performLongClick = new Runnable() {
@Override
public void run() {
mHasPerformedLongPress = v.performLongClick();
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
handler.removeCallbacks(performLongClick);
// case 1
v.setTag(mHasPerformedLongPress);
v.performClick();
// case 2
/*if (!mHasPerformedLongPress) {
v.performClick();
}*/
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(mLastMotionX - x) > TOUCH_MAX || Math.abs(mLastMotionY - y) > TOUCH_MAX) {
handler.removeCallbacks(performLongClick);
}
break;
case MotionEvent.ACTION_DOWN:
mHasPerformedLongPress = false;
handler.removeCallbacks(performLongClick);
mLastMotionX = x;
mLastMotionY = y;
handler.postDelayed(performLongClick, delayMillis);
break;
}
return true;
}
});
}
https://blog.csdn.net/lili625/article/details/78467180
安卓自定义长按事件(延长响应时间)
最新推荐文章于 2024-05-15 09:21:10 发布