我的导航栏未在 Android Studio Kotlin 中的片段之间切换

时间:2021-01-31 22:44:47

标签: android kotlin android-fragments android-architecture-navigation navigationview

  1. 活动
class ChangeInfoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_change_info)

        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        val appBarConf = AppBarConfiguration(setOf(

            R.id.navigation_person, R.id.navigation_chat

        ))

        setupActionBarWithNavController(navController, appBarConf)
        navView.setupWithNavController(navController)

    }

}
  1. 布局
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/backgroundsecond"
    android:layout_height="match_parent"
    tools:context=".ChangeInfoActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/nav_shape"
        app:itemIconTint="@color/red"
        app:itemTextColor="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_review" />

    <fragment
        android:id="@+id/nav_host_fragment"
        app:navGraph="@navigation/nav_graph"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
  1. nav_graph
<?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/personFragment">
    <fragment
        android:id="@+id/personFragment"
        android:name="com.example.autentification.fragments.PersonFragment"
        android:label="fragment_person"
        tools:layout="@layout/fragment_person" >
        <action
            android:id="@+id/action_personFragment_to_chatFragment"
            app:destination="@id/chatFragment" />
    </fragment>
    <fragment
        android:id="@+id/chatFragment"
        android:name="com.example.autentification.fragments.ChatFragment"
        android:label="fragment_chat"
        tools:layout="@layout/fragment_chat" >
        <argument
            android:name="nameofuser"
            app:argType="string"
            android:defaultValue="unnamed warrior" />
    </fragment>
</navigation>
  1. 菜单
<?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/navigation_person"
        android:icon="@drawable/ic_baseline_person_24"
        android:title="Person" />

    <item
        android:id="@+id/navigation_chat"
        android:icon="@drawable/ic_baseline_message_24"
        android:title="Chat"/>

</menu>

似乎我一切都做得很好,但我仍然无法在 2 个片段之间切换。 (P.S 第一次在这个平台提问 不好意思)

1 个答案:

答案 0 :(得分:1)

您需要将 navGraph 中的片段 ID 更改为与菜单的 ID 相同

将其应用于 navGraph

<?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/navigation_person">

    <fragment
        android:id="@+id/navigation_person"
        android:name="com.example.autentification.fragments.PersonFragment"
        android:label="fragment_person"
        tools:layout="@layout/fragment_person" >
        <action
            android:id="@+id/action_personFragment_to_chatFragment"
            app:destination="@id/chatFragment" />
    </fragment>
    <fragment
        android:id="@+id/navigation_chat"
        android:name="com.example.autentification.fragments.ChatFragment"
        android:label="fragment_chat"
        tools:layout="@layout/fragment_chat" >
        <argument
            android:name="nameofuser"
            app:argType="string"
            android:defaultValue="unnamed warrior" />
    </fragment>
</navigation>
相关问题