如何导航到导航抽屉项目上的活动单击 [英] How to navigate to activity on navigation drawer item click
问题描述
这是我的主要活动,我想导航到我的导航抽屉项目单击上的一个活动.我已经检查了实现此目标的旧方法,即使用switch语句和onNavigationItemSelected方法.我知道这是一个经常被问到的问题,但是在阅读了许多关于堆栈溢出的问题和解决方案后,我感到很困惑.对于片段以及单击导航抽屉中的某个项目来启动活动所需的条件,我感到困惑.
This is my main activity i want to navigate to an activity on my navigation drawer item click. I have checked the old way of achieving this i.e using switch statement and using onNavigationItemSelected method. I know this is a question frequently asked, but after reading the many questions and solutions on stack overflow I am confused. I am confused with regards to Fragments and what is required to start an activity from clicking an item in the navigation drawer.
MainActivity.java
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
mobile_navigation.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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.vdosol.navigationdrawer.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_gallery"
android:name="com.vdosol.navigationdrawer.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.vdosol.navigationdrawer.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
<fragment
android:id="@+id/nav_tools"
android:name="com.vdosol.navigationdrawer.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
<fragment
android:id="@+id/nav_share"
android:name="com.vdosol.navigationdrawer.ui.share.ShareFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_share" />
<fragment
android:id="@+id/nav_send"
android:name="com.vdosol.navigationdrawer.ui.send.SendFragment"
android:label="@string/menu_send"
tools:layout="@layout/fragment_send" />
</navigation>
推荐答案
您可以执行以下操作:
MenuItem logOutItem = navigationView.getMenu().findItem(R.id.nav_logout);
logOutItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//do your stuff
return true;
}
});
您的AppBarConfiguration和Nav Controller不应包含上述ID,即"nav_logout"(在我的情况下):
your AppBarConfiguration and Nav Controller should not contain above id ie "nav_logout" (in my case):
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_plan, R.id.navigation_history, R.id.navigation_about, R.id.navigation_feedback, R.id.navigation_notifications)
.setDrawerLayout(drawer)
.build();
您的菜单应仅包含在菜单文件中....无需将其添加到AppBarConfiguration和NavController中....
your menu should only contain in your menu file....no need to add it to you AppBarConfiguration and your NavController....
希望这对您有所帮助....编码愉快
hope this helps....happy coding
这篇关于如何导航到导航抽屉项目上的活动单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!