简体   繁体   English

如何将项目添加到 Android Studio 的导航抽屉模板并为其设置侦听器?

[英]How to add an item to Android Studio's Navigation Drawer template and set Listener for it?

I cant find where is navigationView.setNavigationItemSelectedListener used in that sample.我找不到该示例中使用的navigationView.setNavigationItemSelectedListener在哪里。 I want to link that item to a fragment like what's done on the template!我想将该项目链接到一个片段,就像在模板上所做的那样! Is there any other way to set setNavigationItemSelectedListener without overriding this method?有没有其他方法可以在不覆盖此方法的情况下设置setNavigationItemSelectedListener

You use你用

navigationView.setNavigationItemSelectedListener(this);

inside onCreate.在 onCreate 里面。 And to answer your question I don't think so.要回答你的问题,我不这么认为。

YourActivity implements NavigationView.OnNavigationItemSelectedListener {...}

Inside onCreate在 onCreate 里面

//handle drawer logic

    navigationView.setNavigationItemSelectedListener(this);

Then the navigation listener然后是导航监听器

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        use item.getItemId to handle the clicks
drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Check for and close the drawer in OnBackPressed as well.检查并关闭 OnBackPressed 中的抽屉。 I'm assuming you know how to load your fragment.我假设您知道如何加载片段。

The default template at least on Android Studio 4.0.1 uses Navigation Component, which is meant to replace the implementation you meant in your question and the one I showed.至少在 Android Studio 4.0.1 上的默认模板使用导航组件,它旨在替换您在问题中所指的实现和我展示的实现。 So If you want to use navigation component refer to this link所以如果你想使用导航组件参考这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android Studio的导航抽屉模板中添加图标? - How do I add icons to Android Studio's Navigation Drawer Template? Android Studio Navigation Drawer 项打开网站 - Android Studio Navigation Drawer item opening website 如何在Android应用程序中将ViewPager添加到导航抽屉项? - How can I add a ViewPager to a Navigation Drawer Item in Android application? 如何在android导航抽屉中更改所选项目的背景颜色? - How to change the selected item's background color in android navigation drawer? 如何在运行时在由 android 工作室创建的导航抽屉中设置默认片段 - how to set a default fragment at runtime in Navigation drawer that is created by android studio 我在项目单击侦听器上的导航抽屉不起作用 - My Navigation Drawer on item click listener not worked 如何更改导航抽屉及其项目android studio的图标 - How can I change the icon of the navigation drawer and its item android studio 导航抽屉:如何使项目将您引导至网站。 安卓工作室 - Navigation drawer: how to make the item direct you to a website. Android Studio 如何在Android工具栏下方设置导航抽屉? - How to set the Navigation Drawer below the toolbar in Android? 如何在 Android Studio 3.5 - 最新版本中将选项卡式活动添加到导航抽屉片段中 - How To Add Tabbed Activity Into Navigation Drawer Fragment in Android Studio 3.5 - the latest version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM