我用导航组件设置了一个底部导航视图。片段之间的用户导航工作正常。
问题是通过底部导航视图的导航不会播放导航组件中配置的动画,即,在正确触摸卡片时以幻灯片样式设置动画,单击底部导航视图中的按钮以淡入淡出样式设置动画,覆盖操作在导航组件中定义的属性。
res/menu/bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/home_fragment"
android:title="@string/bottom_navigation_home_title"
android:icon="@drawable/ic_home"
app:showAsAction="ifRoom" />
<item android:id="@+id/schedule_fragment"
android:title="@string/bottom_navigation_schedule_title"
android:icon="@drawable/ic_schedule"
app:showAsAction="ifRoom" />
</menu>
res/anim/slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="700" />
</set>
res/anim/slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="700" />
</set>
res/anim/slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="700" />
</set>
res/anim/slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="700" />
</set>
res/navigation/nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="@id/home_fragment">
<fragment
android:id="@+id/home_fragment"
android:name="com.sslabs.whatsappcleaner.ui.HomeFragment"
android:label="home_fragment"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_home_fragment_to_schedule_fragment"
app:destination="@id/schedule_fragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:popUpTo="@id/home_fragment" />
</fragment>
<fragment
android:id="@+id/schedule_fragment"
android:name="com.sslabs.whatsappcleaner.ui.ScheduleFragment"
android:label="schedule_fragment"
tools:layout="@layout/fragment_schedule">
</fragment>
</navigation>
res/layout/fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<com.google.android.material.card.MaterialCardView
android:id="@+id/home_schedule_card"
android:layout_width="344dp"
android:layout_height="148dp"
app:cardBackgroundColor="@android:color/holo_blue_dark"
app:rippleColor="@android:color/holo_orange_dark" />
</layout>
资源/布局/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_height="0dp"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
com.sslabs.whatsappcleaner.ui.MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val navController: NavController = Navigation.findNavController(this, R.id.nav_host_fragment)
NavigationUI.setupWithNavController(binding.bottomNavigation, navController)
}
}
com.sslabs.whatsappcleaner.ui.HomeFragment
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentHomeBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_home, container, false)
binding.homeScheduleCard.setOnClickListener {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToScheduleFragment())
}
return binding.root
}
}