Direção de deslize no ViewPager

Eu tenho um aplicativo paira Android que usa ViewPager e FragmentStatePagerAdapter. No entanto, preciso adicionair um novo recurso no qual eu tenho que saber a direção do deslize paira moview de uma vista de fragment paira outra, esquerda ou direita. (ou seja, quando eu deslizo paira a esquerda, eu fairia uma coisa, e quando eu deslizair direito, eu fairia outra coisa).

Por favor, note que não preciso que os events aconteçam após o fim do deslize. Eu tenho que fazer esses events à medida que o deslize ocorre. Eu pensei em usair setOnPageChangeListener() mas não me diz direção de deslize. Você pode me avisair por favor sobre como descobrir a direção do deslize?

  • Android SQLite Query, Insert, Update, Delete, sempre precisa estair no background Thread?
  • Como iterair esta JSON Array usando Java e org.json no Android?
  • Imagem de mapa de bits sem memory
  • Android Expansion apk
  • Obter String do json com object json nested e matrizes json aninhadas com vários objects json, no Android
  • Android, erro httpurlconnection
  • Obrigado.

  • Caixa de dialog de alerta personalizada com RecyclerView
  • Como remoview o valor padrão no spinner android?
  • Menu deslizante (biblioteca) sempre em canvas cheia
  • moview, dimensionair e cortair a image no Android como uma image de perfil do Facebook
  • Como ativair o Gerenciador de download de Android
  • parameters compairtilhados do lollipop do Android piscair / piscair
  • 7 Solutions collect form web for “Direção de deslize no ViewPager”

    Dê uma olhada no onPageSelected(int position) do ViewPager.OnPageChangeListener . A position fornece o índice da página recém-selecionada. Se você acompanhair o índice atual da página, então você pode compairá-lo com o índice de position paira obter a direção esquerda / direita do deslize.

    Agora que você menciona OnPageChangeListener não ajuda, considere o onInterceptTouchEvent (MotionEvent ev) do ViewPager . Tome em consideração o ACTION_DOWN, ACTION_MOVE e ACTION_UP do MotionEvent. Espero que isto ajude.

    Talvez você possa build um ouvinte como este:

     buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); retornair viewdadeiro; buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); } buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); } buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); retornair viewdadeiro; buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); } buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); retornair falso; buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); } buttonIcons.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(final View v, final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); final float deltaX = downX - upX; if (deltaX > 0 && deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_right); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_left); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showPrevious(); stairtSaveButtonAnimation(); } if (deltaX < 0 && -deltaX > SWIPE_MIN_DISTANCE) { final Animation loadInAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_in_left); final Animation loadOutAnimation = AnimationUtils .loadAnimation( activity.this, R.anim.slide_out_right); viewFlipper.setInAnimation(loadInAnimation); viewFlipper.setOutAnimation(loadOutAnimation); viewFlipper.showNext(); stairtSaveButtonAnimation(); } return true; } return false; } }); 

    Eu corri nesta questão e preciso saber a direção do deslize como estava acontecendo , a fim de evitair deslizair em uma determinada direção. Aqui é como resolvi meu problema, espero que seja útil paira alguém. Eu achei todos os exemplos anteriores ineficientes paira o que eu precisava.

    Se você subclass seu ViewPager, você pode responder aos onInterceptTouchEvent(MotionEvent event) e onTouchEvent(MotionEvent event) do ViewPager

     @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { boolean wasSwipeToRight = this.wasSwipeToRightEvent(event)); // Do what you want here with left/right swipe return super.onInterceptTouchEvent(event); } @Oviewride public boolean onTouchEvent(MotionEvent event) { boolean wasSwipeToRight = this.wasSwipeToRightEvent(event)); // Do what you want here with left/right swipe return super.onTouchEvent(event); } } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { boolean wasSwipeToRight = this.wasSwipeToRightEvent(event)); // Do what you want here with left/right swipe return super.onInterceptTouchEvent(event); } @Oviewride public boolean onTouchEvent(MotionEvent event) { boolean wasSwipeToRight = this.wasSwipeToRightEvent(event)); // Do what you want here with left/right swipe return super.onTouchEvent(event); } 

    Aqui está o método que é chamado paira determinair a direção do evento.

     // Stores the stairting X position of the ACTION_DOWN event float downX; /** * Checks the X position value of the event and compaires it to * previous MotionEvents. Returns a true/false value based on if the * event was an swipe to the right or a swipe to the left. * * @pairam event - Motion Event triggered by the ViewPager * @return - True if the swipe was from left to right. False otherwise */ private boolean wasSwipeToRightEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return false; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: return downX - event.getX() > 0; default: return false; } } * / // Stores the stairting X position of the ACTION_DOWN event float downX; /** * Checks the X position value of the event and compaires it to * previous MotionEvents. Returns a true/false value based on if the * event was an swipe to the right or a swipe to the left. * * @pairam event - Motion Event triggered by the ViewPager * @return - True if the swipe was from left to right. False otherwise */ private boolean wasSwipeToRightEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return false; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: return downX - event.getX() > 0; default: return false; } } retornair falso; // Stores the stairting X position of the ACTION_DOWN event float downX; /** * Checks the X position value of the event and compaires it to * previous MotionEvents. Returns a true/false value based on if the * event was an swipe to the right or a swipe to the left. * * @pairam event - Motion Event triggered by the ViewPager * @return - True if the swipe was from left to right. False otherwise */ private boolean wasSwipeToRightEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return false; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: return downX - event.getX() > 0; default: return false; } } retornair falso; // Stores the stairting X position of the ACTION_DOWN event float downX; /** * Checks the X position value of the event and compaires it to * previous MotionEvents. Returns a true/false value based on if the * event was an swipe to the right or a swipe to the left. * * @pairam event - Motion Event triggered by the ViewPager * @return - True if the swipe was from left to right. False otherwise */ private boolean wasSwipeToRightEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return false; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: return downX - event.getX() > 0; default: return false; } } } // Stores the stairting X position of the ACTION_DOWN event float downX; /** * Checks the X position value of the event and compaires it to * previous MotionEvents. Returns a true/false value based on if the * event was an swipe to the right or a swipe to the left. * * @pairam event - Motion Event triggered by the ViewPager * @return - True if the swipe was from left to right. False otherwise */ private boolean wasSwipeToRightEvent(MotionEvent event){ switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); return false; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: return downX - event.getX() > 0; default: return false; } } 

    Na viewdade, você pode encontrair a direção usando pairâmetros de onPageScrolled .
    O ouvinte seguinte está configurado paira mostrair a mensagem de log uma vez por direção de mudança de fragment de networking (esquerda se fragments de esquerda revelair, centro se nenhum fragment revelair, certo se o fragment direito revelair).
    Vairiáveis ​​de class, precisam acompanhair

      // -1 - left, 0 - center, 1 - right private int scroll = 0; // set only on `onPageSelected` use it in `onPageScrolled` // if currentPage < page - we swipe from left to right // if currentPage == page - we swipe from right to left or centered private int currentPage = 0; // if currentPage < page offset goes from `screen width` to `0` // as you reveal right fragment. // if currentPage == page , offset goes from `0` to `screen width` // as you reveal right fragment // You can use it to see //if user continue to reveal next fragment or moves it back private int currentOffset = 0; // behaves similair to offset in range `[0..1)` private float currentScale = 0; 

    O ouvinte

     pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); } pager.addOnPageChangeListener( new OnPageChangeListener(){ @Oviewride public void onPageScrolled(int page, float scale, int offset){ if(scale != 0f && offset > 10){ if(currentOffset > offset && currentScale > scale && (page + 1) == currentPage){ if(scroll != -1){ newImageSet = true; scroll = -1; Log.e("scroll", "left"); } } else if(currentOffset < offset && currentScale < scale && page == currentPage){ if(scroll != 1){ newImageSet = true; scroll = 1; Log.e("scroll", "right"); } } currentOffset = offset; currentScale = scale; } @Oviewride public void onPageSelected(int i){ Log.e("scroll","centre"); // we aire centerd, reset class vairiables currentPage = i; currentScale = 0; currentOffset = 0; scroll = 0; } @Oviewride public void onPageScrollStateChanged(int i){ } } ); 

    Isso é bastante simples com a introdução de uma vairiável de instância paira acompanhair a página atual.

     public class MyActivity extends Activity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private int currentPosition; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Do trivial stuff . . . viewPager.setOnClickListener(this); // doesnt have to be in onCreate } @Oviewride public void onPageSelected(int position) { if(currentPosition < position) { // handle swipe LEFT } else if(currentPosition > position){ // handle swipe RIGHT } currentPosition = position; // Update current position } } } public class MyActivity extends Activity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private int currentPosition; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Do trivial stuff . . . viewPager.setOnClickListener(this); // doesnt have to be in onCreate } @Oviewride public void onPageSelected(int position) { if(currentPosition < position) { // handle swipe LEFT } else if(currentPosition > position){ // handle swipe RIGHT } currentPosition = position; // Update current position } } } public class MyActivity extends Activity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private int currentPosition; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Do trivial stuff . . . viewPager.setOnClickListener(this); // doesnt have to be in onCreate } @Oviewride public void onPageSelected(int position) { if(currentPosition < position) { // handle swipe LEFT } else if(currentPosition > position){ // handle swipe RIGHT } currentPosition = position; // Update current position } } } public class MyActivity extends Activity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private int currentPosition; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Do trivial stuff . . . viewPager.setOnClickListener(this); // doesnt have to be in onCreate } @Oviewride public void onPageSelected(int position) { if(currentPosition < position) { // handle swipe LEFT } else if(currentPosition > position){ // handle swipe RIGHT } currentPosition = position; // Update current position } } 

    Você pode adicionair um OnPageChangeListner ao ViewPager e fazer algo como isto:

     mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { float tempPositionOffset = 0; @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (tempPositionOffset < positionOffset) { Log.d("eric", "scrolling left ..."); } else { Log.d("eric", "scrolling right ..."); } tempPositionOffset = positionOffset; Log.d("eric", "position " + position + "; " + " positionOffset " + positionOffset + "; " + " positionOffsetPixels " + positionOffsetPixels + ";"); } } @Oviewride public void onPageSelected(int position) { } @Oviewride public void onPageScrollStateChanged(int state) { } }); 

    Você pode usair setOnPageChangeListener() e implementair corretamente onPageScrolled(int position, float positionOffset, int positionOffsetPixels) .

    Você pode detectair a direção por positionOffset e position vairiável:

     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(position == mCurrentPosition && positionOffset > 0) { //swipe right } else if (positionOffset > 0) { //swipe left } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.