How can I set a long click listener on a MenuItem?
I tried this answer, but the method doesn\'t exist for me. Any solutions?
Code:
<
This isn't perfect at all, but still better than everyone is suggesting here, because it works on whole item layout, not only on actionview's part.
Notice:
index is the position of an item, BUT it also contains position of: HeaderLayout, Divider, Category (Sub menu header)Well, here is the code:
val navigationView = findViewById(R.id.navigation_view)
val navigationRecycler = navigationView[0] as RecyclerView // core-ktx extension for ViewGroup
val navigationLayoutManager = navigationRecycler.layoutManager as LinearLayoutManager
val navigationAdapter = navigationRecycler.adapter
if (navigationAdapter != null) {
navigationRecycler.post { // this line is important
for (index in 0 until navigationAdapter.itemCount) {
val item = navigationLayoutManager.findViewByPosition(index)
item?.setOnLongClickListener {
Toast.makeText(this, "finally!", Toast.LENGTH_SHORT).show()
true
}
}
}
}