[Android 9.0 Pie Java] RecyclerView Press and hold to display a context menu whose contents change for each element.

I couldn't find an article on how to display a context menu that changes the contents of each element by long-pressing the element in RecyclerView, so I'll take a note!

↓ Completed image ↓ 7c30d4aa9804094e0e83ddb1b9247cb8.gif

Implementation method

Context menu display

For Activity or Fragment

View.OnCreateContextMenuListener

Implements.

SampleFragment.java


public class SampleFragment extends Fragment
        implements View.OnCreateContextMenuListener {

There are other patterns to implement in ViewHolder, I wanted to separate the contents 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);

//Attaching Adapter etc. is omitted

registerForContextMenu(rv); //Postscript

Will be added. registerForContextMenu can be called by implementing View.OnCreateContextMenuListener. You can make the context menu 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 and
        //Treat it as the index of the List displayed in RecyclerView
        //Acquire user ID and realize separate 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 numerical value is set in the second argument of the add method, which is used in the handling of the listener when pressed. For reference only, the getPosition () method is provided in the Adapter as follows so that the position of the long-pressed element can be obtained.

Adapter.java


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

        //abridgement

        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 feels like it is sorted by 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.

I hope it helps someone! !!

Recommended Posts

[Android 9.0 Pie Java] RecyclerView Press and hold to display a context menu whose contents change for each element.
[For beginners] Minimum sample to display RecyclerView in Java
[Android] Change the app name and app icon for each Flavor
How to check for the contents of a java fixed-length string