简体   繁体   English

Android 喷气背包组件 - 在导航抽屉点击烤面包

[英]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”时显示祝酒词,但没有显示... 我的导航抽屉..当用户单击 id 为“nav_share”的订单历史记录时,我想显示 toast

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.

相关问题 Android Jetpack 导航 - 带抽屉项的自定义操作 - Android Jetpack Navigation - Custom Action with Drawer Item Android Jetpack 导航组件条件导航问题 - Android Jetpack Navigation component Condtional navigation issue Android抽屉导航单击事件 - Android drawer navigation click events 使用Android Jetpack导航组件与Android BottomAppBar - Using Android Jetpack Navigation component with Android BottomAppBar 如何用Android Navigation Component实现Navigation Drawer - How to implement Navigation Drawer with Android Navigation Component Android jetpack 导航组件结果来自对话框 - Android jetpack navigation component result from dialog Android Jetpack Navigation组件与BottomAppBar菜单交互 - Android Jetpack Navigation component and BottomAppBar menu interaction 如何使用 Android Jetpack 导航组件导航到菜单项单击的片段 - How to navigate to a Fragment on menu item click using Android Jetpack Navigation component 使用带有喷气背包导航组件的导航抽屉时如何更改工具栏图标(汉堡图标) - how to change toolbar icon (hamburger icon) when using navigation drawer with jetpack navigation component Android导航架构组件 - 导航抽屉图标 - Android Navigation Architecture Component - Nav Drawer Icons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM