How to navigate to a Fragment on menu item click using Android Jetpack Navigation component

霸气de小男生 提交于 2020-06-01 06:11:25

问题


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


回答1:


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"

add menu in res-> menu folder

main_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>

MainActivity

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>

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>



回答2:


You can try something this

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
            inflater.inflate(R.menu.menu_home, menu)
            var item: MenuItem = menu.findItem(R.id.actionABC)
            item.actionView.findViewById<AppCompatImageView>(R.id.ivScheduledTrip).setOnClickListener {

            }
            item = menu.findItem(R.id.actionABC)
            item.actionView.findViewById<AppCompatImageView>(R.id.ivShareRide).setOnClickListener {

            }
            super.onCreateOptionsMenu(menu, inflater)
        }



回答3:


YOU CAN TRY ,,

public boolean onNavigationItemSelected(MenuItem item) {
    Fragment newFragment; // This is the fragment you want to put into the FrameLayout

    int id = item.getItemId();
    if (id == R.id.nav_camara) {
        newFragment = new CameraFragment();
    } else if (id == R.id.nav_gallery) {
        newFragment = new GalleryFragment();
    } else if (id == R.id.nav_slideshow) {
        // [...]
    }

    // Let's put the new fragment into the FrameLayout
    // If you use the support action bar, use getSupportFragmentManager(), else getFragmentManager()
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment); // R.id.fragment_container = FrameLayout ID
    transaction.commit();
} 



回答4:


For java, check out https://www.androidauthority.com/android-navigation-architecture-component-908660/. I hope you get more explanation from there.




回答5:


In Kotlin:

Inside onNavigationItemSelected, you’ll find the when block for each menu item. Replace the content inside the when block with the following code:

R.id.nav_inbox -> {
  navController.navigate(R.id.inboxFragment)  //1
}

R.id.nav_sent -> {
  navController.navigate(R.id.sentFragment)  //2
}

R.id.nav_privacy_policy -> {
  navController.navigate(//id)  //3
}

R.id.nav_terms_of_service -> {
  navController.navigate(//id)  //4
}


来源:https://stackoverflow.com/questions/60682102/how-to-navigate-to-a-fragment-on-menu-item-click-using-android-jetpack-navigatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!