如何使用 Android Jetpack Navigation 组件在单击菜单项时导航到 Fragment [英] How to navigate to a Fragment on menu item click using Android Jetpack Navigation component

查看:43
本文介绍了如何使用 Android Jetpack Navigation 组件在单击菜单项时导航到 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过单击菜单项导航到片段.使用 android 导航组件.谢谢

How to navigate to a fragment on the click of the menu item. using the android navigation component.Thanks

推荐答案

我假设您正在使用 kotlinOne Activity 模式,请执行以下步骤

I assume you are using kotlin and One Activity pattern ,do the following steps

build.gradle

implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha03"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha03"

在 res-> 菜单文件夹中添加菜单

add menu in res-> menu folder

ma​​in_menu.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/fragment_two"
    android:title="Fragment Two"
    />
</menu>

主活动

class MainActivity : AppCompatActivity() {
lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    navController = findNavController(this, R.id.nav_host_fragment)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.main_menu, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}

}

activity_main.xml

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/main_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

在 res->navigationmian_graph.xml

in res->navigation mian_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"
android:id="@+id/main_graph"
app:startDestination="@id/fragment_one">

<fragment android:id="@+id/fragment_one"
    android:label="FragmentOne"
    android:name="com.mohammedalaamorsi.test.FragmentOne" />// here you should put the path for your fragment


<fragment android:id="@+id/fragment_two"
    android:label="FragmentTwo"
    android:name="com.mohammedalaamorsi.test.FragmentTwo" />// here you should put the path for your fragment

</navigation>

这篇关于如何使用 Android Jetpack Navigation 组件在单击菜单项时导航到 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆