Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom RecyclerView supporting ContextMenu & ContextMenuInfo
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ContextMenu;
/**
* A RecycleView which binds Extra context menu information and overrides {@link
* ContextMenuRecyclerView#getContextMenuInfo()} to provide the ContextMenuInfo object instead of
* default null ContextMenuInfo in {@link android.view.View#getContextMenuInfo()}
*/
public class ContextMenuRecyclerView extends RecyclerView {
private ContextMenu.ContextMenuInfo mContextMenuInfo;
public ContextMenuRecyclerView(Context context) {
super(context);
}
public ContextMenuRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContextMenuRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected ContextMenu.ContextMenuInfo getContextMenuInfo() {
return mContextMenuInfo;
}
/**
* Used to initialize before creating context menu and Bring up the context menu for this view.
*
* @param position for ContextMenuInfo
*/
public void openContextMenu(int position) {
if (position >= 0) {
final long childId = getAdapter().getItemId(position);
mContextMenuInfo = createContextMenuInfo(position, childId);
}
showContextMenu();
}
ContextMenu.ContextMenuInfo createContextMenuInfo(int position, long id) {
return new RecyclerContextMenuInfo(position, id);
}
/**
* Extra menu information provided to the {@link android.view.View
* .OnCreateContextMenuListener#onCreateContextMenu(android.view.ContextMenu, View,
* ContextMenuInfo) } callback when a context menu is brought up for this RecycleView.
*/
public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo {
/**
* The position in the adapter for which the context menu is being displayed.
*/
public int position;
/**
* The row id of the item for which the context menu is being displayed.
*/
public long id;
public RecyclerContextMenuInfo(int position, long id) {
this.position = position;
this.id = id;
}
}
}
@resengupta

This comment has been minimized.

Copy link
Owner Author

@resengupta resengupta commented Oct 15, 2015

you can use it in the same way by passing your RecyclerView to:
registerForContextMenu(myRecyclerView);

And when ever your menu item click happens on recycler view: myRecyclerView.openContextMenu(clickedItemPosition);

To use the menuInfo object:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
        menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);

    ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView
            .RecyclerContextMenuInfo) menuInfo;
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView
            .RecyclerContextMenuInfo) item
            .getMenuInfo();
}
@akashjaindev

This comment has been minimized.

Copy link

@akashjaindev akashjaindev commented Mar 1, 2017

hello
can you please provide some demo for this. It will be really helpfull.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment