Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Position of ContextMenu Selected in RecycleView

Normally for ListViews, I would do this when I wanted to get the position that the user clicked on a Context Menu.

public boolean onContextItemSelected(android.view.MenuItem item) {

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    position = info.position;

However, ever since I switched to a RecycleView, I now get a null pointer here.

The code above is in my main Activity (Fragment) while onCreateContextMenu() is done in the adapter as per the new way.

ItemView.setOnCreateContextMenuListener(this); is also done in the adapter (specifically the constructor).

like image 645
TheLettuceMaster Avatar asked Jan 04 '16 18:01

TheLettuceMaster


People also ask

What is context menu android?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

How do I get the selected item position in the adapter?

You do not have to use any extra method. Just create a global variable named 'position' and initialize it with getAdapterPosition() in any of the major method of the adapter (class or similar). Here is a brief documentation from this link. getAdapterPosition added in version 22.1.

How do you get the position of the Recycler view?

The RecyclerView. ViewHolder class provides us a method called getAdapterPosition which will return the proper position of anything that currently drawn on the RecyclerView. So, the way I fix the problem I had is by changing the position into viewHolder. getAdapterPosition() .

What is the main function of the RecyclerView?

RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed. As the name implies, RecyclerView recycles those individual elements.


1 Answers

There're 3 options:

  1. You can pass getAdapterPosition() instead of MenuItem's order

    private class ChipViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
        public ChipViewHolder(View itemView) {
            super(itemView);
            itemView.setOnCreateContextMenuListener(this);
        }
    
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.setHeaderTitle("Select The Action");
            menu.add(0, ACTION_1_ID, getAdapterPosition(), "action 1");
            menu.add(0, ACTION_2_ID, getAdapterPosition(), "action 2");
        }
    }
    

    And then, in Activity listen to onContextItemSelected() and retrieve position by getOrder()

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int clickedItemPosition = item.getOrder();
        // do something!
        return super.onContextItemSelected(item);
    }
    
  2. Use custom implementations of RecyclerView like Teovald/ContextMenuRecyclerView one

  3. Setting MenuItem's clickListener (see https://stackoverflow.com/a/33179957/1658267 ) and handles it there.

Yes, it's very inconvenient API. You can choose the one you like most.

like image 193
Konstantin Loginov Avatar answered Sep 20 '22 20:09

Konstantin Loginov