Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick on ViewPager not triggered

I set a click listener on a ViewPager, but the onClick event is never called. I guess the touch event detection of the ViewPager is interfering, but I can't see how to solve it...

Anybody could help?

Thanks

mViewPager.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {         // never called     } });  
like image 952
jul Avatar asked Apr 20 '12 09:04

jul


1 Answers

I solved a similar problem by using a GestureDetector

Sending the MotionEvent to the GestureDetector

tapGestureDetector = new GestureDetector(this, new TapGestureListener());  viewPager.setOnTouchListener(new OnTouchListener() {         public boolean onTouch(View v, MotionEvent event) {             tapGestureDetector.onTouchEvent(event);             return false;         } }); 

It you are using the compatibility library, you can change the first line to:

tapGestureDetector = new GestureDetectorCompat(this, new TapGestureListener()); 

You can handle your Event in the GestureListener:

        class TapGestureListener extends GestureDetector.SimpleOnGestureListener{           @Override          public boolean onSingleTapConfirmed(MotionEvent e) {            // Your Code here          }         } 
like image 125
Martin Christmann Avatar answered Oct 08 '22 08:10

Martin Christmann