Fragmenty Androida i animacje

Jak należy zaimplementować rodzaj przesuwu, którego używa np. Klient Gmaila Honeycomb?

Może TransactionManager obsłużyć to automatycznie dodając i usuwając fragmenty, trochę trudno to przetestować ze względu na emulator będący pokazem slajdów:)

Author: Programmer Bruce, 2011-01-27

5 answers

Aby animować przejście między fragmentami lub animować proces wyświetlania lub ukrywania fragmentu, użyj Fragment Manager, aby utworzyć Fragment Transaction.

W każdej transakcji Fragment można określić animacje wejścia i wyjścia, które będą używane odpowiednio do pokazywania i ukrywania (lub obu w przypadku zamiany).

Poniższy kod pokazuje, w jaki sposób można zastąpić fragment, wysuwając jeden fragment i przesuwając drugi w jego miejsce.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragment = DetailsFragment.newInstance();

ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");

// Start the animated transition.
ft.commit();

Aby osiągnąć to samo z ukrywanie lub Pokazywanie fragmentu po prostu wywołujesz ft.show lub ft.hide, przekazując odpowiednio Fragment, który chcesz pokazać lub ukryć.

Dla odniesienia, definicje animacji XML będą używać znacznika objectAnimator. Przykład slide_in_left może wyglądać mniej więcej tak:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>
 360
Author: Reto Meier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-27 17:43:01

Jeśli nie musisz korzystać z biblioteki wsparcia, spójrz naRoman ' s answer.

Ale jeśli chcesz użyć support library {[14] } musisz użyć starego frameworka animacji, jak opisano poniżej.

Po konsultacji Reto i blindstuff odpowiedzi dostałem następujący kod działa.

Fragmenty pojawiają się przesuwając się w prawo i przesuwając się w lewo gdy z tyłu jest wciśnięty.

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);

CustomFragment newCustomFragment = CustomFragment.newInstance();
transaction.replace(R.id.fragment_container, newCustomFragment );
transaction.addToBackStack(null);
transaction.commit();

Kolejność jest ważna. oznacza to, że musisz wywołać setCustomAnimations() przed replace(), inaczej animacja nie będzie działać!

Następnie te pliki należy umieścić w folderze res/anim.

wejść.xml :

<?xml version="1.0" encoding="utf-8"?>
<set>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromXDelta="100%"
               android:toXDelta="0"
               android:interpolator="@android:anim/decelerate_interpolator"
               android:duration="@android:integer/config_mediumAnimTime"/>
</set>

wyjście.xml :

<set>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromXDelta="0"
               android:toXDelta="-100%"
               android:interpolator="@android:anim/accelerate_interpolator"
               android:duration="@android:integer/config_mediumAnimTime"/>
</set>

pop_enter.xml :

<set>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromXDelta="-100%"
               android:toXDelta="0"
               android:interpolator="@android:anim/decelerate_interpolator"
               android:duration="@android:integer/config_mediumAnimTime"/>
</set>

pop_exit.xml :

<?xml version="1.0" encoding="utf-8"?>
<set>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
               android:fromXDelta="0"
               android:toXDelta="100%"
               android:interpolator="@android:anim/accelerate_interpolator"
               android:duration="@android:integer/config_mediumAnimTime"/>
</set>

Czas trwania animacji można zmienić na dowolny wartości domyślne, takie jak @android:integer/config_shortAnimTime lub dowolna inna liczba.

Zauważ, że jeśli pomiędzy wymianami fragmentów nastąpi zmiana konfiguracji (na przykład obrót), akcja wstecz nie jest animowana. To jest udokumentowany błąd , który nadal istnieje w wersji 20 biblioteki wsparcia.

 221
Author: dmanargias,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:26

Proszę użyć tego myślę o wiele lepszych rozwiązań.Android Studio zapewnia domyślne animation.

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

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            fragmentManager.addOnBackStackChangedListener(this);
            fragmentTransaction.replace(R.id.frame, firstFragment, "h");
            fragmentTransaction.addToBackStack("h");
            fragmentTransaction.commit();

Wyjście:

Tutaj wpisz opis obrazka

 12
Author: Gowthaman M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-03 11:41:32
 5
Author: mark.kedzierski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-20 01:18:09

Rozwiązuję to w sposób poniżej

Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide);
fg.startAnimation(anim);
this.fg.setVisibility(View.VISIBLE); //fg is a View object indicate fragment
 1
Author: Shakawat Hossain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-25 09:03:00