user6150841
user6150841

Reputation:

Custom View and On Touch Listeners

What I am trying to achieve is that I have a custom view. And the view is draggable around the screen.

But how would I display another view when the view is only tapped?

My intial idea on how to achieve my what I need is to see if the view was long clicked then it can be moved and if not display the view.

This is my code:

 collapsedLayout.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();

                    return true;

                case MotionEvent.ACTION_UP:

                    return true;


                case MotionEvent.ACTION_MOVE:
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);
                    mWindowManager.updateViewLayout(entireLayout, params);
                    return true;
            }
            return false;
        }
    });

Thanks in advance for any help!

Upvotes: 0

Views: 2059

Answers (1)

CodeDaily
CodeDaily

Reputation: 766

private static final int THRESHOLD_X = 120;
private static final int THRESHOLD_Y = 120;
private static final int NONE = 0;
private static final int MOVE = 1;
private static int mode = NONE;  

collapsedLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    // Action Down
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(view.getX() > THRESHOLD_X && view.getY()> THRESHOLD_Y){
                        // performDrag
                        mode = MOVE;
                        performDrag(view);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    // Action up
                    if(mode == MOVE){
                        // Do nothing and return
                        return true;
                    }
                    // Perform click to the view
                    view.performClick();
                    break;
                default:
                    // Handle default case.
                    break;
            }
            return false;
        }
    });

Upvotes: 1

Related Questions