setOnCreateContextMenuListener is not working in RecycleView OnBindViewHolder

387 Views Asked by At

I am using setOnCreateContextMenuListener in onBindViewHolder, but the context menu listener is not triggered when I using setOnLongClickListener in the onBindViewHolder.

override fun onBindViewHolder(holder: friendsAdapter.ListViewHolder, position: Int) {    
        holder.tvUsername.text = listFriends[position].username
        holder.tvEmail.text = listFriends[position].email        
        mBundle = Bundle()

        holder.itemView.setOnClickListener{
            mBundle.putString(friendProfile.EXTRA_ID, listFriends[position].id)
            mBundle.putString(friendProfile.EXTRA_USERNAME, listFriends[position].username)
            mBundle.putString(friendProfile.EXTRA_EMAIL, listFriends[position].email)
            mBundle.putString(friendProfile.EXTRA_IMAGE, listFriends[position].profilelink)
            profileFrag.profileData = mBundle
            fManager
                .beginTransaction()
                .replace(R.id.nav_host_fragment, profileFrag, friendProfile::class.java.simpleName)
                .addToBackStack(null)
                .commit()
        }

        holder.itemView.setOnLongClickListener{
            Toast.makeText(context, listFriends[position].id, Toast.LENGTH_SHORT).show()
            mBundle.putString(friendProfile.EXTRA_ID, listFriends[position].id)
            profileFrag.profileData = mBundle
            return@setOnLongClickListener true
        }

        holder.itemView.setOnCreateContextMenuListener(this)
    }
1

There are 1 best solutions below

0
ampalmer On

The comment for the definition for public interface OnLongClickListener tells us:

@return true if the callback consumed the long click, false otherwise.

so in your code, when return@setOnLongClickListener true executes, it tells the listener that the action has been consumed, and not to do anything else with it. So you can put false here (I think) or potentially combine those two methods if you always want to show the context menu in this case (they both use the long click anyway), using an anonymous method like this:

                holder.itemView.setOnCreateContextMenuListener { menu, v, menuInfo ->
                Toast.makeText(context, listFriends[position].id, Toast.LENGTH_SHORT).show()
                mBundle.putString(friendProfile.EXTRA_ID, listFriends[position].id)
                profileFrag.profileData = mBundle

                // Anything else you want to do here, i.e. add menu items like this:
                menu?.add("Action")?.setOnMenuItemClickListener {
                    // Perform action when this menu item is clicked
                    true // Indicates when the user clicks this item that we don't want to do anything else with the menu
                }
                menu?.add("Another action")?.setOnMenuItemClickListener {
                    // Do something else here
                    true
                }
            }

With this method you don't need to have menu resources for the context menu or to implement OnCreateContextMenuListener anywhere.