0

I have a TextView with a swipe listener on it. I wanted to add a long click listener as well. They both work but when I swipe, the long click listener is triggered as well, which is not what I want. I need them to be independent. I've found some very old code about detecting long press but it's not very clear what's the best approach.

here's what I have in my MainActivity

tvIta.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
                @Override
                public void onSwipeRight() {changeSays(); }
                @Override
                public void onSwipeLeft() { changeSays(); }
            });

tvIta.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String label = "Label_Ita";
                String text = tvIta.getText().toString();
                ClipboardManager clipboard = (ClipboardManager) getSystemService(MainActivity.this.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText(label, text);
                clipboard.setPrimaryClip(clip);
                Toast.makeText(MainActivity.this, "Copied to clipboard!", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

this is the swipe listener class

public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public void onSwipeLeft() {
    }

    public void onSwipeRight() {
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_DISTANCE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

// commented out otherwise won't work
//        @Override
//        public boolean onDown(MotionEvent e) {
//            return true;
//        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
}

Thank you!

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
SF1
  • 469
  • 1
  • 6
  • 20
  • 1
    I'd consider testing to see which event fires first and potentially store a state in a boolean and consume it with the opposite listener. Just a thought. I'd be happy to elaborate if nobody has a definitive answer off the top of their heads. – anomeric Jul 28 '17 at 00:24

1 Answers1

0

Try to synchronize both events using booleans. The following code is an example:

boolean swiped = false;
boolean clicked = false;

tvIta.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
            @Override
            public void onSwipeRight() {
                if(!clicked) {
                    swiped = true;
                    changeSays();
                }
            }
            @Override
            public void onSwipeLeft() {
                if(!clicked) {
                    swiped = true;
                    changeSays();
                }
            }
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    swiped = false;
                    clicked = false;
                }
                return super(v, event);
            }
        });

tvIta.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(!swiped) {
                    clicked = true;
                    String label = "Label_Ita";
                    String text = tvIta.getText().toString();
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(MainActivity.this.CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText(label, text);
                    clipboard.setPrimaryClip(clip);
                    Toast.makeText(MainActivity.this, "Copied to clipboard!", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
Keshan Fernando
  • 347
  • 1
  • 4
  • 16
  • this works but there's an issue where, after a swipe, the first long press won't be detected. After a swipe, it requires to touch a first time (does nothing) and then the following long press will be detected – SF1 Jul 28 '17 at 02:36