Анимация перехода между фрагментами
При переключении между фрагментами через DrawerLayout происходит плавная анимация смены фрагмента. Один пропадает, за ним появляется другой.
Но при использовании navController.navigate(....) анимации, как не бывало, топорно и некрасиво переключается. Подскажите как заставить анимацию срабатывать при вызове вручную.
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_slideshow,
R.id.nav_tools,R.id.nav_send, R.id.app_bar_search)
.setDrawerLayout(drawer)
.build();
toggle = new ActionBarDrawerToggle(
this,
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
toggle.setDrawerIndicatorEnabled(true);
//noinspection deprecation
drawer.setDrawerListener(toggle);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
Ответы (1 шт):
Автор решения: Сергей Бувака
→ Ссылка
В NavigationComponent все анимации прописываются в графе фрагментов. Например:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_graph.xml"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.wispapp.themovie.ui.main.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_main_to_details"
app:destination="@id/movieDetailsFragment"
app:enterAnim="@animator/enter_out_right"
app:exitAnim="@animator/exit_in_left"
app:popEnterAnim="@animator/enter_out_left"
app:popExitAnim="@animator/exit_in_right" />
</fragment>
</navigation>
И собственно когда вы вызываете новый фрагмент, вы указываете id action, вместо id фрагмента
findNavController().navigate(R.id.action_main_to_details)