OnLongPress Increase Time

I have an imageView. I use pan, a pinch for imageView. Sometimes I need to remove imageView. So, I used OnLongClickListener to delete the image. When I use a long click, my image scans. But when I use OnTouchListener for panning, hold on to ImageView OnLongPress, and my ImageView is removed from the view. How to solve this?

code:

imageView.setOnLongClickListener(new OnLongClickListener(){ @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub imageView.setVisibility(View.GONE); return true; } }); imageView.setOnTouchListener(new View.OnTouchListener() { final Handler handler = new Handler(); Runnable mLongPressed = new Runnable() { public void run() { Log.i("", "Long press!"); } }; @Override public boolean onTouch(View v,MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_DOWN) handler.postDelayed(mLongPressed, 1000); if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP)) handler.removeCallbacks(mLongPressed); layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN : { parms = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); dx = event.getRawX() - parms.leftMargin; dy = event.getRawY() - parms.topMargin; } break; case MotionEvent.ACTION_MOVE : { x = event.getRawX(); y = event.getRawY(); parms.leftMargin = (int) (x-dx); parms.topMargin = (int) (y - dy); imageView.setLayoutParams(parms); } break; case MotionEvent.ACTION_UP : { } break; } return false; } }); } }); 
+4
source share
4 answers

You can use a handler for this, but you need to forget to cancel the handler if the user moves his finger away from the screen. Yogesh is not entirely wrong, but the approach above simply adds a delay of 1000 ms between onClick and when runnable is executed. This means that if the user lifts his finger, running will work. This is not a real long press.

Below you can see that I am still using a handler with a delay of 1000 ms (you can set it the way you want), but delete the callback if the user lifts his finger or moves. If you want to get rid of the move trigger, just delete this part of the call. But in order to influence the long press, you need to consider the lift so that the user holds his finger all the time.

 final Handler handler = new Handler(); Runnable mLongPressed = new Runnable() { public void run() { Log.i("", "Long press!"); } }; @Override public boolean onTouchEvent(MotionEvent event, View v){ if(event.getAction() == MotionEvent.ACTION_DOWN) handler.postDelayed(mLongPressed, 1000); if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP)) handler.removeCallbacks(mLongPressed); return false; } 
+9
source

try the code below: -

 imageView.setOnClickListener(new View.OnClickListener() Handler handle = new Handler(); handle.postDelayed(new Runnable() { @Override public void run() { imageView.setVisibility(View.GONE); },1000); } 

1000 is the time you can increase as you wish ..

+4
source

onTouch always called for your view, since this is the initial state of sending events to the view. When you long press your opinion still calls onTouch first , and since you return true to onTouch (this means that you used this event and you no longer need to send it), you will not get onLongPress called. What will the returning false trick do onTouch

0
source

I know that the answer was chosen some time ago, but for those who are looking for a solution that actually prolongs the long print time, the solution is to use Boolean in onKeyUp () to prevent your code from executing in the executable. Thus, a single KeyUp event does not trigger a reset, and does not just delay the runnable. I used key events, but this solution should work for touch events as well.

 private static final int longPressMilli = 3000; boolean allowReset = true; Runnable mLongPressed = new Runnable() { @Override public void run() { if(allowReset) { sendResetIntent(); Log.d(TAG, "Trip Reset!"); allowReset = false; } } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub //System.out.println("" + keyCode); if(GlobalBooleans.getReverseCamera()) return true; switch (keyCode) { case KeyEvent.KEYCODE_F6: allowReset = true; //reset to true if u need to able to reset multiple times without navigating away from the page pressHandler.postDelayed(mLongPressed, longPressMilli); break; default: break; } // } return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_F6: allowReset = false; break; } return super.onKeyUp(keyCode, event); } 
0
source

Source: https://habr.com/ru/post/1488077/


All Articles