i want to popup context Menu on long click RecyclerView items using Kotlin code
Asked
Active
Viewed 3,728 times
2
-
Create a callback in the view holder class of Recycler view adapter when item.onLongClickListener is called. In the activity, just implement this callback function. – devDeejay Sep 11 '19 at 12:25
-
check this : https://stackoverflow.com/a/57320757/7666442 – AskNilesh Sep 11 '19 at 12:25
2 Answers
3
You can try like this in onBindViewHolder
cvCarIcon
can be replaced with rootlayout of your row. or even you can handle with long clicklistener
holder.cvCarIcon.setOnClickListener{
val popup = PopupMenu(mContext, holder.cvCarIcon)
popup.inflate(R.menu.options_menu)
popup.setOnMenuItemClickListener(object : PopupMenu.OnMenuItemClickListener() {
fun onMenuItemClick(item: MenuItem): Boolean {
when (item.getItemId()) {
R.id.menu1 ->
return true
else -> return false
}
}
})
popup.show()
}

Deepak S. Gavkar
- 447
- 8
- 21
2
I solved this by adding this code to ViewHolder class to show popup menu with two items delete and cancel with resource file(menu xml file) named click_menu
init{
V.tV.setOnLongClickListener {
// V is View variable and tv is name of textView
val pop= PopupMenu(V.context,it)
pop.inflate(R.menu.click_menu)
pop.setOnMenuItemClickListener {item->
when(item.itemId)
{
R.id.delete->{ }
R.id.cancel->{ }
}
true
}
pop.show()
true
}
}

memo
- 71
- 1
- 5