我一直在关注NavigationBasicSample
中的示例但使用了最新的导航库:
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha05"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha05"
和androidx库:
implementation 'androidx.core:core-ktx:1.0.0-rc01'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'com.google.android.material:material:1.0.0-rc01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
并添加到bundle.gradle
androidExtensions {
experimental = true
}
除动画外的所有作品。可能是错误还是?
这是我的导航图:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@+id/categoryFragment">
<fragment
android:id="@+id/categoryFragment"
android:name="com.sample.CategoriesFragment"
android:label="Categories"
tools:layout="@layout/fragment_categories">
<action
android:id="@+id/action_categoryFragment_to_itemsFragment"
app:destination="@id/itemsFragment"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left" />
</fragment>
<fragment
android:id="@+id/itemsFragment"
android:name="com.sample.ItemsFragment"
android:label="Items"
tools:layout="@layout/fragment_items" >
<argument android:name="cat_id" android:defaultValue="-1" />
</fragment>
</navigation>
和我的导航主机片段:
<fragment
android:id="@+id/fragment_nav_host"
app:defaultNavHost="true"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
答案 0 :(得分:3)
我认为您使用此代码:
findNavController(it, R.id. fragment_nav_host).navigate(R.id. itemsFragment)
如果这是真的,那就错了。您必须使用在动画中编写的动作。
正确的将是:
findNavController(it,R.id.fragment_nav_host).navigate(R.id.action_categoryFragment_to_itemsFragment)