Here is my problem, I have this architecture of class :
- Activity
- Fragment
- RecyclerView (which has an Adapter)
- Fragment
The adapter:
has a class ViewHolder:
public static class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener{ TextView noteName; public NoteViewHolder(View itemView) { super(itemView); noteName = (TextView)itemView.findViewById(R.id.note_name); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {/**/} }); itemView.setOnCreateContextMenuListener(this); } @Override public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle("Select The Action"); contextMenu.add(0,666,0,"Delete Note"); }
}
The fragment
overrrides onContextItemSelected(MenuItem item){
//info is null
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
}
It also calls
registerForContextMenu(recyclerView);
The sequence
- I long click on the NoteViewHolder, it opens the context menu
- I select "Delete"
- OnCreateContextMenu is called with contextMenuInfo == null
- onSelectedContextItem is called with item.getMenuInfo() == null
How can i get the menuInfo non null?
Shoud i create myself the menu info, and if so where?
PS, I've seen this post: How to create context menu for RecyclerView , i don't see the answer to this
PS2 I've also read this: http://androidheight.blogspot.fr/2015/03/recyclerview-widget-example-in-android.html, and to me the line new RecyclerAdapter().info = menuInfo;
seems wrong
thank you