Stop OnLongClickListener from firing while dragging

后端 未结 3 813
忘了有多久
忘了有多久 2020-12-17 20:46

I have a custom View with bitmaps on it that the user can drag about.

I want to make it so when they long click one of them I can pop up a context menu with options

3条回答
  •  情深已故
    2020-12-17 21:44

    //This code is to handle the gestures detection

    final Handler handler = new Handler();
    private Runnable mLongPressRunnable;
    
    detector = new GestureDetector(this, new MyGestureDectector());
    view.setOnTouchListener(new OnTouchListener() {
    
            @SuppressLint("ClickableViewAccessibility")
            @SuppressWarnings("deprecation")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                detector.onTouchEvent(event);
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
    
                    handler.postDelayed(mLongPressRunnable, 1000);
                }
                if ((event.getAction() == MotionEvent.ACTION_MOVE)
                        || (event.getAction() == MotionEvent.ACTION_UP)) {
                    handler.removeCallbacks(mLongPressRunnable);
    
                    }
    
                }
    
                return true;
            }
        });
    mLongPressRunnable = new Runnable() {
            public void run() {
                Toast.makeText(MainActivity.this, "long", Toast.LENGTH_SHORT)
                        .show();
            }
        };
    class MyGestureDectector implements GestureDetector.OnDoubleTapListener,
            OnGestureListener {
            //Implement all the methods
            }
    

提交回复
热议问题