How to set a long click listener on a MenuItem (on a NavigationView)?

前端 未结 6 1580
无人及你
无人及你 2020-12-11 01:49

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:

<         


        
6条回答
  •  没有蜡笔的小新
    2020-12-11 02:26

    From massivemadness answer here is a slight variation to target a menu. I also don't think this is stable, it feels like a bodge but nest it in a try catch, keep calm and move on :)

    if(navigationAdapter != null) {
                navigationRecycler.post { // this line is important
                    for(index in 0 until navigationAdapter.itemCount) {
                        val item = navigationLayoutManager.findViewByPosition(index)
                        item?.setOnLongClickListener {
                            try{
                                Toast.makeText(this, "${nav_view.menu[index - 1 /*Number of children before your menu*/].title}!", Toast.LENGTH_SHORT).show()
                                true
                            } catch (e:Throwable){
                                false
                            }
    
    
                        }
                    }
                }
            }
    
    

提交回复
热议问题