How to combine OnClickListener and OnTouchListener for an ImageButton

后端 未结 5 520
一整个雨季
一整个雨季 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:31

    There are many solutions for this problem, the best one for me is to eliminate totally the setOnClickListener and inside the onTouchListener do check if the action is ACTION_UP and then inside that check if the last before that is ACTION_DOWN:

    case MotionEvent.ACTION_UP:
        if (lastAction == MotionEvent.ACTION_DOWN) {
            //code for click   
            .
            .
            lastAction = event.getAction();
        }
    

    lastAction is int and I update it at the end of each ACTION

提交回复
热议问题