[英]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.