Skip to content

Instantly share code, notes, and snippets.

@resengupta
Last active March 26, 2022 10:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resengupta/2b2e26c949b28f8973e5 to your computer and use it in GitHub Desktop.
Save resengupta/2b2e26c949b28f8973e5 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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