Android Jetpack组件-导航抽屉上的吐司单击

时间:2019-11-21 09:48:44

标签: android android-jetpack

我正在使用导航jetpack并设置了导航抽屉。一切正常。但是问题是我想在用户单击“ nav_share”时显示一个吐司,但是没有显示... my nav drawer..i want to show toast when user clicks order history whose id is "nav_share"

这是我制作导航抽屉的方式

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);

我的导航抽屉菜单是

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

最后:

@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;
    }

我想要点击吐司...我看不到我在想什么......

4 个答案:

答案 0 :(得分:0)

使用此

 NavigationView navigationView = findViewById(R.id.nav_view);
 navigationView.bringToFront()
 navigationView.setNavigationItemSelectedListener(this);

答案 1 :(得分:0)

如果有人想要答案..i做了一些研究,终于找到了解决方案...希望它能帮助您...

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;
     }
 });

答案 2 :(得分:0)

如果有人仍在寻找有关如何绑定导航目标并处理导航抽屉菜单项单击的答案,请在此处回答:HttpServletResponse.sendError()

答案 3 :(得分:-1)

我也遇到了您的问题,并提出了解决方案

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;
        }
    });