[Java] [Android 9.0 Pie Java] RecyclerView Show context menu with different contents by long press

2 minute read

I did not find an article on how to display the context menu whose contents change for each element by long-pressing the element in RecyclerView, so I will write it down!

↓ Completion image ↓ 7c30d4aa9804094e0e83ddb1b9247cb8.gif

Implementation method

Display context menu

To Activity or Fragment

View.OnCreateContextMenuListener

Implements

SampleFragment.java


public class SampleFragment extends Fragment
        implements View.OnCreateContextMenuListener {

There is also a pattern to implement in ViewHolder, I wanted to separate out the content of the context menu for each element, so I implemented it in a convenient Fragment.

Next, in the onCreatedView method that is creating the RecyclerView

SampleFragment.java


RecyclerView rv = view.findViewById(R.id.recyclerView);

//Attachment such as Adapter is omitted

registerForContextMenu(rv); // add

Will be added. registerForContextMenu can be called by implementing View.OnCreateContextMenuListener. The context menu can be recognized by this description.

Next, implement onCreateContextMenu in Fragment.

SampleFragment.java


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        // Get the long pressed message position,
        // Treat as the index of List displayed in RecyclerView,
        // Acquire the user ID and implement different menus
        if (list.get(adapter.getPosition()).getUserId() == loginUserId) {
            menu.add(Menu.NONE, 1, Menu.NONE, "copy");
            menu.add(Menu.NONE, 2, Menu.NONE, "edit");
            menu.add(Menu.NONE, 3, Menu.NONE, "delete");
        } else {
            menu.add(Menu.NONE, 1, Menu.NONE, "copy");
        }
    }

You can easily add items by adding the content you want to add to the ContextMenu object. A number is set in the second argument of the add method, which is used in the handling of the listener when it is pressed. For reference only, we have prepared the getPosition() method in the Adapter as follows, so that the position of the element that has been long-pressed can be obtained.

Adapter.java


    @Override
    public void onBindViewHolder(final ChatViewHolder holder, int position) {

        // omitted

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                setPosition(holder.getPosition());
                return false;
            }
        });
    }

    // get the position of the long-pressed element
    public int getPosition() {
        return position;
    }

    // set the position of the long-pressed element
    public void setPosition(int position) {
        this.position = position;
    }

With the implementation so far, the context menu can be displayed.

Context menu event listener

Implement onContextItemSelected and handle it using a switch statement as shown below. Since you can get the numerical value for distinguishing each element registered earlier with MenuItem.getItemId(), It’s like a switch statement.

SampleFragment.java


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                // copy TODO message to clipboard
                return true;
            case 2:
                // edit TODO message
                return true;
            case 3:
                // delete TODO message
                return true;
            default:
                return false;
        }
    }

that’s all.

We hope you find it helpful to anyone! !!