2017-10-01 5 views
1

Je veux atteindre un comportement de transition de fragment spécifique, comme je le mentionne dans la question, je veux que mon fragment apparaisse de la droite et disparaît dans le même sens jusqu'au point il commence à partir .. ce sont mes animations xml:La transition de fragment apparaît de droite à gauche, disparaît de gauche à droite (retour au point de départ)

right_to_left.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" > 
<translate 
    android:duration="1000" 
    android:fromXDelta="100%" 
    android:fromYDelta="0%" 
    android:toXDelta="0%" 
    android:toYDelta="0%" /> 

left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate 
    android:duration="1000" 
    android:fromXDelta="0%" 
    android:fromYDelta="0%" 
    android:toXDelta="-100%" 
    android:toYDelta="0%" /> 

et voici les différentes façons que j'ai essayé de réaliser l'animation fragment:

Lorsque le fragment apparaît:

productHolder.fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable(Utils.productDetailsKey, productPojo); 
       ProductDetailsFragment productDetailsFragment = new ProductDetailsFragment(); 
       productDetailsFragment.setArguments(bundle); 
       FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
       FragmentTransaction transaction = fragmentManager.beginTransaction(); 
       transaction.setCustomAnimations(R.anim.product_animation_enter, R.anim.product_animation_enter); 
       transaction.add(R.id.newContainer, productDetailsFragment); 
       transaction.commit(); 
       MainActivity.transparent.setBackgroundColor(ContextCompat.getColor(context, R.color.blackTransparent)); 


      } 
     }); 

Lorsque le fragment disparaît Méthode 1:

fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
       transaction.setCustomAnimations(R.anim.product_animation_exit, R.anim.product_animation_exit); 
       transaction.remove(ProductDetailsFragment.this); 
       transaction.commitAllowingStateLoss(); 
       MainActivity.transparent.setBackgroundColor(0x00000000); 


      } 
     }); 

Lorsque le fragment disparaît Méthode 2:

fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.product_animation_exit); 
        animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); 
        animation.setAnimationListener(new Animation.AnimationListener() { 
         @Override 
         public void onAnimationStart(Animation animation) { 

         } 

         @Override 
         public void onAnimationEnd(Animation animation) { 
          try { 
           FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
           transaction.remove(ProductDetailsFragment.this); 
           transaction.commitAllowingStateLoss(); 
           MainActivity.transparent.setBackgroundColor(0x00000000); 

          } catch (Exception e) { 
          } 

         } 

         @Override 
         public void onAnimationRepeat(Animation animation) { 

         } 
        }); 

        getView().startAnimation(animation); 
       } 
      }); 

malheureusement aucun de ceux travaillé comme je l'attends, mai cette image peut expliquer plus ce que je veux: image Merci à l'avance.

Répondre

0

Je suggérerais, d'abord vous faire une solution de travail la première partie de votre problème, qui est de basculer entre les fragments, puis vous ajoutez la deuxième partie, qui est l'animation.

1. Changement de fragments

Dans votre fichier de configuration XML que vous prenez tout ce qui appartient aux fragments et ajouter un Framelayout.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

Dans votre code, vous obtenez une référence à la FrameLayout comme ceci:

int containerId = R.id.fragment_container; 

Ensuite, vous avez deux méthodes, l'un pour ajouter le premier fragment et un pour des allers-retours entre les deux.

La méthode add va comme ceci:

void addFrag(int containerId, Fragment firstFragment){ 
     FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(containerId, firstFragment); 
     transaction.commit(); 
} 

La méthode replace va comme ceci:

void replaceFrag(int containerId, Fragment newFragment){ 
     FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.replace(containerId, newFragment); 
     transaction.commit(); 
} 

Tant que vous passez le fragment à droite, vous pouvez utiliser cette méthode pour remplacer un tes fragments avec l'autre. Modifier Vous pouvez également utiliser la méthode replaceFragseulement, même pour ajouter un fragment au conteneur pour la première fois.Fin de modifier

2. Animations

qui est vraiment la partie facile. Vous n'avez pas besoin de créer votre propre XML, car les animations coulissantes sont intégrées dans Android (utilisez donc les identifiants que je fournis ici). Tout ce que vous avez à faire est d'ajouter une ligne de code avant d'ajouter ou de remplacer vos fragments:

transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right); 

Modifier Depuis ces animations intégrées à partir de 50%, cependant, ils ne regardent pas trop gentil, à mon avis. Vous pouvez simplement utiliser vos XML:

transaction.setCustomAnimations (R.anim.left_to_right, R.anim.right_to_left);

Fin de modification