Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My MotionEvent.ACTION_DOWN is not detected

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;
        }
    });
like image 343
SoliQuiD Avatar asked Mar 14 '23 01:03

SoliQuiD


1 Answers

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;
}

}

like image 195
SoliQuiD Avatar answered Mar 19 '23 06:03

SoliQuiD