Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a ContextMenu when the recyclerview item is set onLongClickListener

How can I have a context menu inside the RecyclerViewAdapter when the view is set onLongClickListener?

Here is my code:

 public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

            public TextView name;

            public ViewHolder(View itemLayoutView) {

                super(itemLayoutView);
                name = (TextView) itemLayoutView.findViewById(R.id.rvname);

                itemLayoutView.setOnClickListener(this);
                itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }

                });
            }

Here is the example image that I want to achieve

enter image description here

like image 430
Christine Ramos Avatar asked Sep 20 '16 17:09

Christine Ramos


People also ask

How to use context menu with recycler view?

The best was to use Context menu with recycler view is if you make a custom recycler view and override the getContextMenuInfo () method and return your own instance of Context menu info object so that you can fetch positions when it was created and when menu is clicked: Have a look at this gist I have created:

How to implement onclicklistener and oncontextmenulistener in recycleview?

You can't directly implement these method like onClickListener, OnContextMenuListener etc. because RecycleView extends android.view.ViewGroup. So we cant directly use these method. We can implement these methods in ViewHolder adapter class. We can use context menu in RecycleView like this way:

Why did Google create recyclerview without a click listener?

Some Android developers wonder why Google created a view like Recyclerview without a click listener (given the fact that the deprecated ListView has an item click listener). Because there is no standard way of setting a click listener, new developers tend to confuse on the right way of doing it.

How to create custom menu using oncreatecontextmenulistener in Android?

You can pass OnCreateContextMenuListener into ViewHolder on bind. This listener can create custom menu for each item of data. Just add setOnCreateContextMenuListener in your ViewHolder and call it during binding.


2 Answers

Based on my experience I do not need to have setOnLongClickListener to make the context menu appear, View.OnCreateContextMenuListener will do that for me.

Here's my working code:

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {

    public TextView name;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        name = (TextView) itemLayoutView.findViewById(R.id.rvname);
        itemLayoutView.setOnClickListener(this);
        itemLayoutView.setOnCreateContextMenuListener(this);
    }

    @Override
    public void onClick(View v) {
        String location = name.getText().toString();
        Intent goFlip = new Intent(RecyclerAdapter.context, FlipActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name", location);
        bundle.putInt("pos", getAdapterPosition());
        goFlip.putExtras(bundle);
        context.startActivity(goFlip);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem edit = menu.add(Menu.NONE,1,1,"Edit");
        MenuItem delete = menu.add(Menu.NONE,2,2,"Delete");


        edit.setOnMenuItemClickListener(onChange);
        delete.setOnMenuItemClickListener(onChange);
    }
    private final MenuItem.OnMenuItemClickListener onChange = new MenuItem.OnMenuItemClickListener() {
       @Override
       public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()){
                case 1:
                    Toast.makeText(context,"Edit",Toast.LENGTH_LONG).show();
                    return true;
                case 2:
                    Toast.makeText(context,"Delete",Toast.LENGTH_LONG).show();
                    return true;
            }
           return false;
       }
   };
}

I also want to share the link that I found to make this work: https://gist.github.com/gauravat16/e8e03496a4056829e65dede3c236da28

like image 124
Christine Ramos Avatar answered Sep 30 '22 13:09

Christine Ramos


You need to implement PopupMenu An implementation can be found here

like image 27
Sahil Avatar answered Sep 30 '22 14:09

Sahil