0

I have a ViewPager of images, and I want to make it slide in the following way:
If I click on the right (left) side of the screen, go to previous (next) image. It's like the screen is two halves and each half moves to a direction on click.

How can I implement this?

Omar
  • 7,835
  • 14
  • 62
  • 108

2 Answers2

0

You can get Disaplay width
Then you can override onTouchEvent in your viewpager.
When you get MotionEvent object - you can get 'tap' coordinates and you will be able to understand on wich side of screen user clicked and handle it.

Community
  • 1
  • 1
x90
  • 2,140
  • 15
  • 25
  • Will this interfere with the normal sliding of the ViewPager? – Omar Mar 02 '14 at 16:17
  • It depends. When you override onTouchEvent you should return boolean flag: True if the event was handled, false otherwise. If you will call 'return super.onTouchEvent(event)' - both ways will work (i am not sure but you can try). – x90 Mar 02 '14 at 16:36
0

Set on touch listener for your ViewPager or contentview of ViewPager item.

Take the half of the screen/view width, check if event.getX() is greater than the half value, if so, go to next, otherwise to the previous.

@Override
public boolean onTouch(View v, MotionEvent ev) {
    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        mDownX = ev.getX();
        mDownY = ev.getY();
        isOnClick = true;
    }else if(ev.getAction() == MotionEvent.ACTION_UP ){
        if (isOnClick ) {
               if(ev.getX() > mViewPager.getWidth()/2) {
                  //go to next
               }else{
                 //go to previous
                  }
               return true;

        }
    }else if(ev.getAction() == MotionEvent.ACTION_MOVE){    
     if (isOnClick && (Math.abs(mDownX - ev.getX()) > DRAG_THRESHOLD || Math.abs(mDownY - ev.getY()) > DRAG_THRESHOLD)) {
        isOnClick = false;
        return false;
    }


 }
    return false
}
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148