0

I am in a position to implement different actions for OnClick and OnTouch listeners for an ImageView.I have referred several links in stackoverflow but couldn't achieve my goal. how can I implement this.I've referred

how to use both Ontouch and Onclick for an ImageButton?

Android onTouch with onClick and onLongClick

Can't handle both click and touch events simultaneously

OnTouch/OnClick listeners. Use both?

actually my context was when i click on imageview it will goto other activity and when i swipe on it it has to liked by the user. any help.

Community
  • 1
  • 1
GvSharma
  • 2,632
  • 1
  • 24
  • 30
  • why you want to use ontouch linstener? – CompEng Apr 07 '14 at 12:23
  • to drag the imageview either left or right(left -meaning user Dislikes the item & right - user likes it) and for click action he has to move to other activity to check all the images of that product(item). – GvSharma Apr 07 '14 at 12:28
  • if you use the onTouchListener you can add a gesture detector and override onTap, onLongTap, onFling and the others – Ayoub Apr 07 '14 at 12:28
  • i have used gesturedetector, in that implemented onFling method(swipe right and swipe left used) it is working but onclick is not working at this point... – GvSharma Apr 07 '14 at 12:35

3 Answers3

3
@Override
    public boolean onTouch(View v, MotionEvent event) {

        System.out.println("ajay +event.getAction()" + event.getAction());
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                layOutParams.x = initialX + (int) (event.getRawX() - initialTouchX);
                layOutParams.y = initialY + (int) (event.getRawY() - initialTouchY);

                break;
            case MotionEvent.ACTION_DOWN:
                initialX = layOutParams.x;
                initialY = layOutParams.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) {
                    return false;// to handle Click
                }
                break;
            case MotionEvent.ACTION_UP:
                if (initialTouchX == event.getRawX() && initialTouchY == event.getRawY()) {
                    return false;// to handle Click
                }
                break;

        }
        return true;

    }
};    

onClick Implemenation

overLayview.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("ajay onClick Lisner");
        }
    });
ajay
  • 477
  • 6
  • 14
  • Thank you ajay....I have asked this exactly one year back..(April 7,2014)..I had done this earlier. – GvSharma Apr 07 '15 at 13:49
1

You dont need to implement both behaviors, define MIN_SWIPE_DISTANCE and check at ACTION_UP in onTouch, so you will have something like this.

pseudo

private static final int MIN_SWIPE_DISTANCE=300;
private int oldX,newX;

public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction()==MotionEvent.ACTION_DOWN) // touch down
    {   oldX = event.getX(); //get touch position      }

    else if(event.getAction()==MotionEvent.ACTION_UP) // touch up
    {
       newX = event.getX(); //get last touch position 
       if (Math.abs( newX-oldX )> MIN_SWIPE_DISTANCE)
           //its a swipe
       else
           // its a click
     }
}
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
  • what is oldX and newX? i mean for different screens how can i give different values for different screens? any way Thanks – GvSharma Apr 07 '14 at 12:45
0

I think you will need to decide if the gesture is a swipe or not and act acrodingly

see here how to achieve it https://stackoverflow.com/a/938657/1393632

Community
  • 1
  • 1
asaf gitai
  • 400
  • 2
  • 10