[英]Android jetpack component - toast on navigation drawer click
I am using navigation jetpack and have set up navigation drawer.我正在使用导航喷气背包并设置了导航抽屉。 Every thing works fine.
每件事都很好。 But the problem is I want to show a toast when user clicks "nav_share" but it is not showing...
但问题是我想在用户单击“nav_share”时显示祝酒词,但没有显示...
here is how i made navigation drawer这是我制作导航抽屉的方法
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_plan, R.id.navigation_notifications)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
my menu for navigation drawer is我的导航抽屉菜单是
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/menu_gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_slideshow" />
<item
android:id="@+id/nav_tools"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="@string/menu_send" />
</menu>
</item>
</menu>
finally:最后:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.nav_share)
Toast.makeText(LauncherActivity.this, "Click", Toast.LENGTH_SHORT).show();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
i want that click toast...i cannot see what am i missing....我想要那个点击吐司……我看不到我错过了什么……
if any one wants the answer..i did some research and finally found a solution to it...hope it helps....如果有人想要答案..我做了一些研究,终于找到了解决方案...希望它有所帮助....
NavigationView navigationView = findViewById(R.id.nav_view);
MenuItem shareItem = navigationView.getMenu().findItem(R.id.nav_share);
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(LauncherActivity.this, "click", Toast.LENGTH_SHORT).show();
//do as you want with the button click
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
Use this用这个
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront()
navigationView.setNavigationItemSelectedListener(this);
In case if anyone is still searching for the answer as to how we can tie up navigation destinations and also handle click on menu items of the navigation drawer, it has been answered here: https://stackoverflow.com/a/57846680/3283350如果有人仍在寻找关于我们如何绑定导航目的地并处理单击导航抽屉菜单项的答案,请在此处回答: https://stackoverflow.com/a/57846680/3283350
I also had your problem and came up with the solution我也遇到了你的问题并想出了解决方案
NavController navController = Navigation.findNavController(this, R.id.nav_host_home);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.nav_share) {
Toast.makeText(getApplicationContext(), "nav_share", Toast.LENGTH_SHORT).show();
}
NavigationUI.onNavDestinationSelected(menuItem, navController);
drawerLayout.closeDrawer(Gravity.RIGHT);
return true;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.