Skip to content Skip to sidebar Skip to footer

Ontouchlistener In Viewpager

I have an ImageView with onTouchListener inside a ViewPager. Of course the ImageView's listener fires off when I try to change ViewPager's page. Is there a way to prevent it. I gue

Solution 1:

I'd go with something like this:

publicclassOnSwipeTouchListenerimplementsOnTouchListener {

privatefinalGestureDetectorgestureDetector=newGestureDetector(newGestureListener());

publicbooleanonTouch(final View v, final MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

privatefinalclassGestureListenerextendsSimpleOnGestureListener {

    privatestaticfinalintSWIPE_THRESHOLD=100;
    privatestaticfinalintSWIPE_VELOCITY_THRESHOLD=100;

    @OverridepublicbooleanonDown(MotionEvent e) {
        returntrue;
    }
    @OverridepublicbooleanonSingleTapConfirmed(MotionEvent e) {
        onTouch(e);
        returntrue;
    }


    @OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        booleanresult=false;
        try {
            floatdiffY= e2.getY() - e1.getY();
            floatdiffX= e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                    } else {
                    }
                }
            } else {
               // onTouch(e);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}
publicvoidonTouch(MotionEvent e) {
}
}

and call them this way:

pagerGraph.setOnTouchListener(new OnSwipeTouchListener() {          
publicvoidonTouch(MotionEvent event) {
    // your code here
});

Post a Comment for "Ontouchlistener In Viewpager"