Last active
October 26, 2016 23:38
-
-
Save FrancoisBlavoet/cba0aa150e60b727636d to your computer and use it in GitHub Desktop.
ContextMenuRecyclerView - simple sample on how to adapt an existing ContextMenu to RecyclerView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ContextMenuRecyclerView extends Recycler View { | |
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
//... | |
registerForContextMenu(mList); | |
} | |
////////////////////////////////////////////////////////////////// | |
//// Menu | |
////////////////////////////////////////////////////////////////// | |
@Override | |
public void onContextualMenuClicked(Track track, View view) { | |
//call this callback from your viewholder. | |
getActivity().openContextMenu(view); | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
super.onCreateContextMenu(menu, v, menuInfo); | |
dz.ui.Menu.MenuItem[] currentContextMenuItems = getCurrentContextMenuItems( | |
menu, v, menuInfo); | |
if (currentContextMenuItems != null) { | |
View header = View.inflate(getActivity(), | |
R.layout.context_menu_header, null); | |
menu.setHeaderView(header); | |
// configure the header and add your menuItems | |
} | |
} | |
@Override | |
public boolean onContextItemSelected(MenuItem item) { | |
// Never call super.onOptionsItemSelected(item), otherwise there will be a infinite loop | |
ABaseActivity activity = (ABaseActivity) getActivity(); | |
return activity.onMenuEvent(activity, | |
dz.ui.Menu.getSavedMenuItem(item.getItemId())); | |
} | |
public void customizeContextMenuHeaderView(ContextMenu.ContextMenuInfo menuInfo, RemoteImageView picture, | |
TextView mainText, TextView secondText, TextView thirdTextView) { | |
ContextMenuRecyclerView.RecyclerContextMenuInfo info = | |
(ContextMenuRecyclerView.RecyclerContextMenuInfo) menuInfo; | |
if (mAdapter == null) { | |
return; | |
} | |
int position = info.position; | |
mAdapter.getItem(position); | |
// customize the headerView according to the corresponding item in the adapter (not mandatory) | |
} | |
public Menu.MenuItem[] getCurrentContextMenuItems(ContextMenu menu, View v, | |
ContextMenu.ContextMenuInfo menuInfo) { | |
if (mAdapter == null) { | |
return null; | |
} | |
RecyclerContextMenuInfo info = | |
(ContextMenuRecyclerView.RecyclerContextMenuInfo) menuInfo; | |
int position = info.position; | |
///// do whatever you need to get your MenuItem Array and return it. | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, I needed to get the targetView of the ContextMenuInfo, and it works if you just add support for a
View targetView
inRecyclerContextMenuInfo
So you can access the context menu title. 👍
(For anyone who may need it...)