[英]How to add click event on navigation item drawer in android kotlin?
I want to add click event when I click on one of the item in navigation drawer, I have used onNavigationItemSelected method but it's not working, Any help?当我单击导航抽屉中的一项时,我想添加单击事件,我使用了 onNavigationItemSelected 方法但它不起作用,有帮助吗?
override fun onNavigationItemSelected(item: MenuItem): Boolean {
TODO("Not yet implemented")
val id = item.itemId
if (id == R.id.nav_signout) {
Toast.makeText(this, "Sign out", Toast.LENGTH_SHORT).show()
}
return true
}
drawer.xml抽屉.xml
<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:title="Authentication">
<menu android:checkableBehavior="all">
<item
android:id="@+id/nav_signout"
android:icon="@drawable/ic_menu_gallery"
android:title="Sign out" />
</menu>
</item>
</group>
</menu>
Since you are overriding onNavigationItemSelected I suppose you implemented NavigationView.OnNavigationItemSelectedListener
directly to your activity/fragment.由于您正在覆盖 onNavigationItemSelected 我想您将NavigationView.OnNavigationItemSelectedListener
直接实现到您的活动/片段。
Make sure you added it to your navigation when it is created确保在创建时将其添加到导航中
navigation_view.setNavigationItemSelectedListener(this)
Or other option would be to implement it directly to your navigation instead of activity/fragment.或者其他选择是将其直接实现到您的导航而不是活动/片段。 Remove code you posted and activity/fragment implementation and use kotlin lambdas like this删除您发布的代码和活动/片段实现,并像这样使用 kotlin lambda
navigation_view.setNavigationItemSelectedListener{
TODO("Not yet implemented")
val id = item.itemId
if (id == R.id.nav_signout) {
Toast.makeText(this, "Sign out", Toast.LENGTH_SHORT).show()
}
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.