Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onInterceptTouchEvent's ACTION_UP and ACTION_MOVE never gets called

Tags:

android

Log is never logging ACTION_UP or ACTION_MOVE(which i removed from the code example for shortening)

Here is my shorten version of the code:

 public class ProfileBadgeView extends LinearLayout {

    Activity act;

    public ProfileBadgeView(Context context) {
        super(context);
    }

    public ProfileBadgeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProfileBadgeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void initView(Activity act) {
        //..init
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            logIntercept("ACTION DOWN");
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            logIntercept("ACTION_UP");
        }
        return false;
    }

    @Override
        public boolean onTouchEvent(MotionEvent ev) {
        return true;
}


    private void logIntercept(Object obj) {
        Log.i(this.getClass().getSimpleName() + " INTERCEPT :", obj.toString());
    }



}
like image 573
Adam Varhegyi Avatar asked Sep 17 '25 09:09

Adam Varhegyi


1 Answers

Your onInterceptTouchEvent method is not called after ACTION_DOWN event because you return true in onTouchEvent method. So all the other events are sent in onTouchEvent and not in onInterceptTouchEvent any more:

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

You will receive the down event here. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.

http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

like image 109
Donkey Avatar answered Sep 19 '25 06:09

Donkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!