onClick on ViewPager not triggered

后端 未结 8 1211
有刺的猬
有刺的猬 2020-11-28 04:49

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, bu

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 05:23

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

提交回复
热议问题