Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why I always get ACTION_DOWN from onTouchEvent

Tags:

android

touch

I extend my class from View. and I override onTouchEvent function. But, I can get only ACTION_DOWN event. not any ACTION_UP or ACTION_MOVE. what should I do to get that events ?

I override only onTouchEvent and onDraw function. and there is only this view in application

@Override
public boolean onTouchEvent(MotionEvent event) {
    System.out.println("event:"+event.getAction());
    return super.onTouchEvent(event);
}
like image 501
Adem Avatar asked Dec 03 '11 15:12

Adem


2 Answers

Possibly you need to return true instead of returning the super event. That means that you handled that event, and thus you can receive the next one.

like image 55
Peterdk Avatar answered Oct 18 '22 06:10

Peterdk


At work we had a LinearLayout called teaser which was having similar issues (it's been a while). Anyway, we found that this "hack" seems to help get some extra responsiveness out of a touch.

teaser.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent motionEvent) {
        TransitionDrawable transition = (TransitionDrawable) v.getBackground();

        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(transition != null) transition.startTransition(250);
                break;
            case MotionEvent.ACTION_UP:
                loadArticle(position);
            case MotionEvent.ACTION_CANCEL:
                if(transition != null) transition.reverseTransition(0);
                break;
        }
        return false;
    }
});

// Has to be here for the touch to work properly.
teaser.setOnClickListener(null); // <-- the important part for you

I honestly couldn't give you any rhyme or reason to it, but hopefully that helps.

like image 28
Jacksonkr Avatar answered Oct 18 '22 07:10

Jacksonkr