2016-10-05 16 views
7

Implementuję widok BottomNavigation dla systemu Android i mam kilka fragmentów do wyświetlenia jako strony BottomNavigation. Według Google Material Design Guide Lines chcę pokazać fragmenty z animacją cross fade.Animacja Android ViewPager CrossFade

Dotykając elementów BottomNavigation, mój ViewPPager zmienia fragmenty z domyślną animacją slajdów.

Czytałem niektóre rozwiązania w this i this. ale te nie są tak naprawdę animacją blaknięcia i nie mogłem ustawić opóźnienia.

Czy istnieje sposób na ustawienie animacji na zmianie kart ViewPagera?

Odpowiedz

2

W końcu znalazłem odpowiedź.

Zmieniłem ViewPager z układem, aby zachować moje fragmenty (układ ramek). Następnie dodałem fragmenty do fragmentu Transakcji.

Dotykając elementu w BottomNavigation, bieżące ukrywanie fragmentów i nowe fragmenty są wyświetlane z animacją zanikania zdefiniowaną w fragmencie transakcji.

i to jest mój kod:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_page); 

    addFragmentsToManager(); 
} 

private void addFragmentsToManager() { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out); 

    fragmentTransaction.add(R.id.flContent, tripFragment, tripFragment.getClass().getSimpleName()); 
    fragmentTransaction.add(R.id.flContent, notificationFragment, notificationFragment.getClass().getSimpleName()); 
    fragmentTransaction.add(R.id.flContent, searchFragment, searchFragment.getClass().getSimpleName()); 
    fragmentTransaction.add(R.id.flContent, profileFragment, profileFragment.getClass().getSimpleName()); 

    fragmentTransaction.hide(tripFragment); 
    fragmentTransaction.hide(notificationFragment); 
    fragmentTransaction.hide(searchFragment); 
    fragmentTransaction.hide(profileFragment); 
    fragmentTransaction.commit(); 
} 

private void changeTab(int position) { 
    Fragment fragment; 
    switch (position) { 
     fragment = .....// get framgnet from position 
    } 

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out); 

    fragmentTransaction.hide(prvFragment); 
    fragmentTransaction.show(fragment).commit(); 
    prvFragment = fragment; 
} 
1

Jest problem z dodawaniem i ukrywanie fragmentów.

Gdy aplikacja jest bezczynna, a telefon przechodzi w tryb uśpienia, po ponownym włączeniu do aplikacji wszystkie fragmenty wyświetlane w działaniu i wszystkie układy są widoczne w jednym.

+0

Tak, istnieją i nie mogę znaleźć sposobu na rozwiązanie problemu – FarshidABZ