How to combine OnClickListener and OnTouchListener for an ImageButton

后端 未结 5 568
一整个雨季
一整个雨季 2020-12-10 01:12

I need to do something when the user clicks the ImageButton I\'ve tried to create a static class that implements both OnClickListener

5条回答
  •  星月不相逢
    2020-12-10 01:45

    The onTouchListener is able to handle both movements.

    Switch between event.action() values to obtain MotionEvent.

    case MotionEvent.ACTION_DOWN: is the first finger impact.
    case MotionEvent.ACTION_UP: is when the finger goes away.

    You'll have to set the impact point on ACTION_DOWN.

    TheImpactPoint=event.getX();

    And then obtain the distance with ACTION_UP

    float distance =TheImpactPoint-event.getX();

    If distance = 0 then there is a click, otherwise it would be more or less than zero depending on gesture.

    So this is the way to use the click event without a real click event and using only the onTouchListener.

    Hope will be useful.

提交回复
热议问题