androidandroid-viewpagerandroid-eventandroid-gesture

Right and Left click on ViewPager


How do i detect if i have clicked or press at right side of viewPager or at left side of viewPager. I have tried with GestureDetector but couldn't make it out. Help me


Solution

  • Use the GestureDetector to get SingleTapConfirmed Action

    @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                float x=e.getX()
    
                if(x < ( getWidth() * 0.5 )){
                  //also check if item we set if valid or not i.e (getCurrentItem() - 1) > 0
                  setCurrentItem( getCurrentItem() - 1)
    
                }else{
                  //also check if item we set if valid or not i.e (getCurrentItem() + 1) < maxChildCount
                  setCurrentItem( getCurrentItem() + 1)
    
                }
    
                return true;
            }
    

    add this code in your custom ViewPager