La animación de Viewflipper no funciona en el primer golpe

En mi actividad principal, tengo un viewflipper con tres vistas de niño. Después de que la aplicación se inicie por primera vez, cuando hago el primer desplazamiento de derecha a izquierda, la vista cambia, pero no tiene la animación de diapositivas. Después de la primera pasada, la animación funciona como se espera al pasar en cualquier dirección. Estoy siguiendo este tutorial. El código que utilizo es:

public boolean onTouchEvent(MotionEvent touchevent) { switch (touchevent.getAction()) { // when user first touches the screen case MotionEvent.ACTION_DOWN: { lastX = touchevent.getX(); break; } case MotionEvent.ACTION_UP: { float currentX = touchevent.getX(); // left to right swipe if (lastX < currentX) { if (mViewFlipper.getDisplayedChild() == 0) break; mViewFlipper.setInAnimation(this, R.anim.in_from_left); mViewFlipper.setOutAnimation(this, R.anim.out_to_right); mViewFlipper.showPrevious(); } // right to left swipe if (lastX > currentX) { if (mViewFlipper.getDisplayedChild() == mViewFlipper.getChildCount() - 1) break; mViewFlipper.setInAnimation(this, R.anim.in_from_right); mViewFlipper.setOutAnimation(this, R.anim.out_to_left); mViewFlipper.showNext(); } break; } } return false; } 

Cuando depuro el código, no veo ninguna diferencia entre cuando la animación está trabajando y cuando no lo es. Además, veo este comportamiento en un dispositivo real y el emulador. ¿Qué me perdí? Puedo publicar los archivos xml de la animación y el xml de la vista, si son necesarios.

EDITAR:

La única manera que puedo conseguir que esto funcione como esperado es fijar lo siguiente en el método del onCreate:

  mViewFlipper.setInAnimation(this, R.anim.in_from_right); mViewFlipper.setOutAnimation(this, R.anim.out_to_left); mViewFlipper.setFlipInterval(10000); mViewFlipper.startFlipping(); 

Entonces llamo a stopFlipping () en el primer golpe. Lo interesante para mí es que la animación funciona en el primer golpe con estos cambios, incluso si no se ha producido la primera respuesta automática. Sin embargo, si simplemente configuro la animación en el método onCreate sin llamar al método startFlipping (), todavía no tiene la animación en el primer golpe. ¿Puede alguien ofrecer una explicación de por qué se produce este comportamiento?

Mirando a través del código fuente de la clase ViewFlipper, ViewFlipper.showNext () llama internamente al método ViewAnimator.showOnly (int position).

Esta es la validación realizada dentro de ese método:

  void showOnly(int childIndex) { final boolean animate = (!mFirstTime || mAnimateFirstTime); showOnly(childIndex, animate); } 

Por lo tanto, para lograr lo que quieres, deberás decirle al ViewFlipper para animar la primera vuelta dentro de tu Activity.onCreate:

  @Override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); mViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper); mViewFlipper.setAnimateFirstView(true); } 

NOTA:

Fui capaz de lograr esto sin llamar a mViewFlipper.setAnimateFirstView (true) usando el nivel de API 22. Pero no parece funcionar igual en las versiones anteriores.

Al principio, usted no tiene true algorythm de su onTouchEvent . Ahora usted consigue algo como la inversión con el ajuste incorrecto de la orden de la animación.

Trate de usar mi onTouchEvent , esto es un trabajo para mí como un encanto:

  public boolean onTouchEvent(MotionEvent touchevent) { switch (touchevent.getAction()) { // when user first touches the screen to swap case MotionEvent.ACTION_DOWN: { lastX = touchevent.getX(); break; } case MotionEvent.ACTION_UP: { float currentX = touchevent.getX(); // if left to right swipe on screen if (lastX < currentX) { // If no more View/Child to flip if (viewFlipper.getDisplayedChild() == 0) break; // set the required Animation type to ViewFlipper // The Next screen will come in form Left and current Screen will go OUT from Right viewFlipper.setInAnimation(this, R.anim.in_from_left); viewFlipper.setOutAnimation(this, R.anim.out_to_right); // Show the next Screen viewFlipper.showNext(); } // if right to left swipe on screen if (lastX > currentX) { if (viewFlipper.getDisplayedChild() == 1) break; // set the required Animation type to ViewFlipper // The Next screen will come in form Right and current Screen will go OUT from Left viewFlipper.setInAnimation(this, R.anim.in_from_right); viewFlipper.setOutAnimation(this, R.anim.out_to_left); // Show The Previous Screen viewFlipper.showPrevious(); } break; } } return true; } 
 Try to use this gesture for swipe with animation just get animation swipe left to right and right to left and put inside this: final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i("null", "onFling has been called!"); final int SWIPE_MIN_DISTANCE = 80; final int SWIPE_MAX_OFF_PATH = 150; final int SWIPE_THRESHOLD_VELOCITY = 100; try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { try { //your swipe code } catch (Exception e) { n = -1; } } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { try { //your swipe code } catch (Exception e) { } } } catch (Exception e) { } return super.onFling(e1, e2, velocityX, velocityY); } }); yourFullRelativeLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gesture.onTouchEvent(event); } }); yourView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gesture.onTouchEvent(event); } }); 
  • Creación de un ViewFlipper como la pantalla de inicio con MotionEvent.ACTION_MOVE
  • Hacer carrusel con ViewFlipper o ViewPager
  • Actualizar altura con ViewFlipper
  • Android captura todos los eventos de movimiento y los envía a los niños
  • Android - Control de animación de viewflipper con el dedo / táctil?
  • Android - inAnimation funciona para ViewFlipper pero outAnimation no
  • Cómo cambiar entre actividades con viewflipper
  • Hacer diferencias entre setOnClickListener y setOnTouchListener
  • ¿La mejor práctica para viewflipper que contiene 10 linearlayouts?
  • Cómo cargar imágenes de la URL en el widget de la aplicación Android?
  • ¿Cómo implementar un evento Swipe para ver Flipper en un ViewPager?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.