My MotionEvent.ACTION_DOWN is not detected
Asked Answered
H

1

6

My problem is that all touch events are being detected but not the Action_Down event. the log results in events 1, 2 and not 0 (for down).

here is my code

final ViewPager homeViewPager = (ViewPager)findViewById(R.id.homeViewPager);

    homeViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch (View v, MotionEvent event){
                Log.i("ViewPager", "event " + event.getAction());
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        Log.i("ViewPager", "ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_DOWN:
                        Log.i("ViewPager", "ACTION_DOWN");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i("ViewPager", "ACTION_UP ");
                        break;
                }
            return false;
        }
    });
Havener answered 25/11, 2015 at 15:11 Comment(2)
That cant be, all touch actions begins with a action_down, then many action_move until the Action_up finishes the touch.Underwriter
@nanoc thanks ofr trying to help, but i found the solution and wrote itHavener
H
8

I found a good explanation on http://neevek.net/posts/2013/10/13/implementing-onInterceptTouchEvent-and-onTouchEvent-for-ViewGroup.html

simply: viewpager is a viewgroup so by default it passes its (down) touch events to child views

solution i override the class with this and used my viewpager class

public class MyViewPager extends ViewPager {
public MyViewPager(Context context) {
    super(context);
}

public MyViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return true;
}

}

Havener answered 25/11, 2015 at 15:22 Comment(1)
Glad you got the solution however, for some classes which can't be overriden e.g. ViewPager2 we still need to depend on setOnTouchListener. Let me know any solution for the sameHook

© 2022 - 2024 — McMap. All rights reserved.