Вопрос

I have imageView. Im using pan,pinch for imageView. Sometimes i need to delete imageView. So, i used OnLongClickListener for remove image. When i use long click my imageView removing. But when i use OnTouchListener for pan,pinch for imageView OnLongPress activated and my imageView removed from 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;
       }
       });

      }

   });
Это было полезно?

Решение

You can use a handler to do this but you need to remember to cancel the handler if the user takes their finger off the screen. Yogesh is not totally wrong but the approach above simply adds a 1000 ms delay between onClick and when the runnable is executed. That means if the user lifts their finger the runnable will still run. This is not a true longpress.

Below you can see that I'm still using a handler with a 1000 ms delay (you can set it to whatever you want) but remove the callback if the user lifts their finger up or moves. If you want to get rid of the move trigger just delete that part of the call. But to affect long press you need to account for the lift to ensure the user holds their finger the whole 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;    
}

Другие советы

try the below code :-

imageView.setOnClickListener(new View.OnClickListener()
    Handler handle = new Handler();
            handle.postDelayed(new Runnable() {

                @Override
                public void run() {
    imageView.setVisibility(View.GONE);

},1000);
}

1000 is time you can increase as you want..

onTouch is always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still calls onTouch first and since you return true in onTouch(which means that you've consumed this event and it should not be further dispatched) you won't get onLongPress called. What will do the trick is returning false in onTouch

I know an answer was chosen a while ago but for anyone who's looking for a solution to actually extend the long press time, the solution is to use a Boolean in onKeyUp() to prevent your code in your runnable from executing. This way a single KeyUp event wont cause a reset and not just delay the runnable. I used keyevents 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);
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top