如何为RecyclerView创build上下文菜单

如何为RecyclerView?实现上下文菜单RecyclerView? 显然调用registerForContextMenu(recyclerView)不起作用。 我从一个片段调用它。 有没有人实现这个成功?

你好,你不能像onClickListenerOnContextMenuListener等直接实现这些方法,因为RecycleView扩展android.view.ViewGroup,所以我们不能直接使用这些方法。 我们可以在ViewHolder适配器类中实现这些方法。 我们可以像这样使用RecycleView中的上下文菜单

 public static class ViewHolder extends RecyclerView.ViewHolder implements OnCreateContextMenuListener { TextView tvTitle; ImageView ivImage; public ViewHolder(View v) { super(v); tvTitle =(TextView)v.findViewById(R.id.item_title); v.setOnCreateContextMenuListener(this); } 

现在我们按照相同的过程,而实现上下文菜单。

 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("Select The Action"); menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title menu.add(0, v.getId(), 0, "SMS"); } 

如果您有任何疑问,请发表评论。 自豪地成为一个安卓:)

感谢您的信息和意见。 我能够为Recyclerview项目实现ContextMenu

这是我做的

在片段的onViewCreated方法或Activity的onCreate方法中:

 registerForContextMenu(mRecyclerView); 

然后在适配器添加

 private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } 

使ViewHolder类实现OnCreateContextMenuListener

 public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public ImageView icon; public TextView fileName; public ImageButton menuButton; public ViewHolder(View v) { super(v); icon = (ImageView)v.findViewById(R.id.file_icon); fileName = (TextView)v.findViewById(R.id.file_name); menuButton = (ImageButton)v.findViewById(R.id.menu_button); v.setOnCreateContextMenuListener(this); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //menuInfo is null menu.add(Menu.NONE, R.id.ctx_menu_remove_backup, Menu.NONE, R.string.remove_backup); menu.add(Menu.NONE, R.id.ctx_menu_restore_backup, Menu.NONE, R.string.restore_backup); } } 

onBindViewHolder方法在OnLongClickListener上添加OnLongClickListener以在上下文菜单加载之前捕获位置:

 holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { setPosition(holder.getPosition()); return false; } }); 

然后在onViewRecycled删除监听器,以便没有参考问题。 (可能不需要)。

 @Override public void onViewRecycled(ViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } 

最后在Fragment / Activity中覆盖onContextItemSelected如下所示:

 @Override public boolean onContextItemSelected(MenuItem item) { int position = -1; try { position = ((BackupRestoreListAdapter)getAdapter()).getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.ctx_menu_remove_backup: // do your stuff break; case R.id.ctx_menu_restore_backup: // do your stuff break; } return super.onContextItemSelected(item); } 

目前的答案是不正确的。 这是一个工作实现:

 public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Override protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Override public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

在您的片段(或活动)中:

 @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.my_context_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); RecyclerViewContextMenuInfo info = (RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu item here } 

最后,在您的ViewHolder中:

 class MyViewHolder extends RecyclerView.View.ViewHolder { ... private void onLongClick() { itemView.showContextMenu(); } } 

试试这个在recycleView中的View项目

 .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add("delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { //do what u want return true; } }); } }); 

您可以使用它将数据设置到ViewHolder项目

Prabhakar 答案是正确的,但他没有解释,如何select一个上下文菜单项,如何获得与被按下的项目相关的数据。 我们可以使用onContextItemSelectedcallback,但在这种情况下, ContextMenuInfo不可用( null )(如果getContextMenuInfo()方法没有被覆盖的按下视图)。 所以,最简单的解决scheme是直接添加OnMenuItemClickListenerMenuItem

 private class ViewHolder extends RecyclerView.ViewHolder { private final TextView mTitleTextView; private MyItemData mData; public ViewHolder(View view) { super(view); mTitleTextView = (TextView)view.findViewById(R.id.title); view.setOnCreateContextMenuListener(mOnCreateContextMenuListener); } public void bind(@NonNull MyItemData data) { mData = data; String title = mData.getTitle(); mTitleTextView.setText(title); } private final View.OnCreateContextMenuListener mOnCreateContextMenuListener = new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { if (mData!= null) { MenuItem myActionItem = menu.add("My Context Action"); myActionItem.setOnMenuItemClickListener(mOnMyActionClickListener); } } }; private final MenuItem.OnMenuItemClickListener mOnMyActionClickListener = new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { //todo: process item click, mData is available here!!! return true; } }; } 

@ Renaud的答案为我工作,但需要先修改几个代码。 这就像他从他的代码几个不同的迭代发布片段。 需要做出的改变是:

  • RecyclerContextMenuInfoRecyclerViewContextMenuInfo是相同的类。 选一个名字,并坚持下去。
  • ViewHolder必须实现View.OnLongClickListener ,并记得在构造函数的item上调用setOnLongClickListener()
  • onLongClick()监听器中, getView().showContextMenu()是完全错误的。 您必须在ContextMenuRecyclerView调用showContextMenuForChild() ,否则您在onCreateContextMenu()onContextItemSelected()获取的ContextMenuInfo将为null。

我编辑的代码如下:

ContextMenuRecyclerView:

 public class ContextMenuRecyclerView extends RecyclerView { private RecyclerViewContextMenuInfo mContextMenuInfo; @Override protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Override public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerViewContextMenuInfo(longPressPosition, longPressId); return super.showContextMenuForChild(originalView); } return false; } public static class RecyclerViewContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerViewContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

在你的片段中:

 @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mRecyclerView = view.findViewById(R.id.recyclerview); registerForContextMenu(mRecyclerView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // inflate menu here // If you want the position of the item for which we're creating the context menu (perhaps to add a header or something): int itemIndex = ((ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo).position; } @Override public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo(); // handle menu here - get item index or ID from info return super.onContextItemSelected(item); } 

在你的ViewHolder中:

 class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener { public MyViewHolder( View itemView ) { super( itemView ); itemView.setOnLongClickListener( this ); } @Override public boolean onLongClick() { recyclerView.showContextMenuForChild( v ); return true; } } 

另外,请确保在布局中用ContextMenuRecyclerView代替RecyclerView

我可能迟到了派对,但我有一个工作的解决scheme 。 我已经为它做了一个要点。

将上下文菜单添加到RecyclerView

ActivityName.java

 //Import Statements public class ActivityName extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_birthdays); //Recycle View mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mLayoutManager = new LinearLayoutManager(getApplicationContext()); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new BirthdaysListAdapter(data, this); mRecyclerView.setAdapter(mAdapter); } 

RecyclerAdapter.java

 //Import Statements public class BirthdaysListAdapter extends RecyclerView.Adapter<BirthdaysListAdapter.ViewHolder> { static Context ctx; private List<typeOfData> Data; public BirthdaysListAdapter(List<typeOfData> list, Context context) { Data = list; this.ctx = context; } BirthdaysListAdapter() { } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { public TextView name; public TextView Birthday; public ImageView colorAlphabet; public TextView textInImg; public ViewHolder(View v) { super(v); name = (TextView) v.findViewById(R.id.name); Birthday = (TextView) v.findViewById(R.id.Birthday); colorAlphabet = (ImageView) v.findViewById(R.id.colorAlphabet); textInImg = (TextView) v.findViewById(R.id.textInImg); v.setOnCreateContextMenuListener(this); //REGISTER ONCREATE MENU LISTENER } @Override public void onCreateContextMenu(ContextMenu menu, View v //CREATE MENU BY THIS METHOD ContextMenu.ContextMenuInfo menuInfo) { new BirthdaysListAdapter().info = (AdapterView.AdapterContextMenuInfo) menuInfo; MenuItem Edit = menu.add(Menu.NONE, 1, 1, "Edit"); MenuItem Delete = menu.add(Menu.NONE, 2, 2, "Delete"); Edit.setOnMenuItemClickListener(onEditMenu); Delete.setOnMenuItemClickListener(onEditMenu); } //ADD AN ONMENUITEM LISTENER TO EXECUTE COMMANDS ONCLICK OF CONTEXT MENU TASK private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { DBHandler dbHandler = new DBHandler(ctx); List<WishMen> data = dbHandler.getWishmen(); switch (item.getItemId()) { case 1: //Do stuff break; case 2: //Do stuff break; } return true; } }; } public List<ViewBirthdayModel> getData() { return Data; } @Override public long getItemId(int position) { return super.getItemId(position); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view_birthdays, parent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.name.setText(Data.get(position).getMan().getName()); holder.Birthday.setText(Data.get(position).getMan().getBday()); holder.colorAlphabet.setBackgroundColor(Color.parseColor(Data.get(position).getColor())); holder.textInImg.setText(String.valueOf(Data.get(position).getMan().getName().toUpperCase().charAt(0))); } @Override public int getItemCount() { return Data.size(); } private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } 

你好,一个出来了一个替代scheme,为我工作。 我只注册我的itemView与registerContextMenu y ViewHolder构造函数,也设置一个onLongClikcListener到相同的视图。 在onLongClick(View v)实现中,我简单地通过getLayoutPosition()获得点击的位置并保存在一个实例variables中(我创build了一个类来表示这个数据,就像ContextMenuInfo预期的那样),但更重要的是使确定你在这个方法中返回false 。 所有你现在要做的就是在onContextItemSelected(MenuItem项)上,读取你存储在你的实例variables中的数据,如果它是有效的,继续你的动作。 这是一个片段。

  public MyViewHolder(View itemView){ super(itemView); registerForContextMenu(itemView); itemView.setOnLongClickListener(this); } 

我使ViewHolder实现了OnLongClickListener,但是你可以用你喜欢的任何方式来完成。

 @Override public boolean onLongClick(View v){ mCurrentLongItem = new ListItemInfo(v.getId(), getLayoutPosition()); return false; // REMEMBER TO RETURN FALSE. } 

你也可以在适配器中设置它,或者在ViewHolder中设置另一个View(即TextView)。 onLongClik()的实现很重要。

 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.client_edit_context_menu: if(mCurrentLongItem != null){ int position = mCurrentLongItem.position; //TAKE SOME ACTIONS. mCurrentLongItem = null; } return true; } return super.onContextItemSelected(item); } 

最好的部分是你仍然可以在你想要的情况下处理LongClick事件返回true,并且conextMenu不会显示出来。

这个方法是可行的,因为registerForContextView使得View LongClickable成为可能,当它处理ContextMenu的时候,系统调用performLongClick,它首先调用onLongClick实现,如果返回false,则调用showContextMenu。

我已经将我的解决scheme与@Hardik Shah的解决scheme结合在一起:

在这个活动中我有:

 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (v.getId() == R.id.rvQuests) { getMenuInflater().inflate(R.menu.list_menu, menu); } } 

在适配器中我有:

 private MainActivity context; private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public QuestsAdapter(MainActivity context, List<Quest> objects) { this.context = context; this.quests.addAll(objects); } public class QuestViewHolder extends RecyclerView.ViewHolder { private QuestItemBinding questItemBinding; public QuestViewHolder(View v) { super(v); questItemBinding = DataBindingUtil.bind(v); v.setOnCreateContextMenuListener(context); } } @Override public void onBindViewHolder(final QuestViewHolder holder, int position) { Quest quest = quests.get(position); holder.questItemBinding.setQuest(quest); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { setPosition(holder.getAdapterPosition()); return false; } }); } @Override public void onViewRecycled(QuestViewHolder holder) { holder.itemView.setOnLongClickListener(null); super.onViewRecycled(holder); } 

在片段中我有:

 @Override public boolean onContextItemSelected(MenuItem item) { int position = ((QuestsAdapter) questsList.getAdapter()).getPosition(); switch (item.getItemId()) { case R.id.menu_delete: Quest quest = questsAdapter.getItem(position); App.getQuestManager().deleteQuest(quest); questsAdapter.remove(quest); checkEmptyList(); return true; default: return super.onContextItemSelected(item); } } 

我一直在努力,因为在RecyclerView中,Android并不能很好地处理这个问题,这对于ListView来说工作得非常好。

最难的是ContextMenuInfo块embedded在一个视图中,除了覆盖视图之外,您不能轻易附加它。

所以你将需要一个包装,可以帮助您将位置信息传递给活动。

 public class RecyclerContextMenuInfoWrapperView extends FrameLayout { private RecyclerView.ViewHolder mHolder; private final View mView; public RecyclerContextMenuInfoWrapperView(View view) { super(view.getContext()); setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); mView = view; addView(mView); } public void setHolder(RecyclerView.ViewHolder holder) { mHolder = holder; } @Override protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return new RecyclerContextMenuInfo(mHolder.getPosition(), mHolder.getItemId()); } public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } 

}

然后,在您的RecyclerAdapter中,创buildViewHolders时,需要将Wrapper设置为根视图,并在每个视图上注册contextMenu。

 public static class AdapterViewHolder extends RecyclerView.ViewHolder { public AdapterViewHolder( View originalView) { super(new RecyclerContextMenuInfoWrapperView(originalView); ((RecyclerContextMenuInfoWrapperView)itemView).setHolder(this); yourActivity.registerForContextMenu(itemView); itemView.setOnCreateContextMenuListener(yourListener); } 

}

最后,在你的活动中,你可以做你平常做的事情:

 @Override public boolean onContextItemSelected(MenuItem item) { int position = ((RecyclerContextMenuInfoWrapperView.RecyclerContextMenuInfo)item.getMenuInfo()).position; // do whatever you need as now you have access to position and id and everything 

最好的做法是使用带有回收视图的上下文菜单,如果您制作自定义回收视图并覆盖getContextMenuInfo()方法并返回自己的上下文菜单信息对象实例,以便在创build菜单时单击该菜单:

 @Override protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } 

看看我创造的这个要点:

https://gist.github.com/resengupta/2b2e26c949b28f8973e5

我已经有一段时间一直在使用这个解决scheme,并且对我来说工作很好。

 public class CUSTOMVIEWNAME extends RecyclerView { public CUSTOMVIEWNAME(Context context) { super(context); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs) { super(context, attrs); } public CUSTOMVIEWNAME (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private RecyclerContextMenuInfo mContextMenuInfo; @Override protected ContextMenu.ContextMenuInfo getContextMenuInfo() { return mContextMenuInfo; } @Override public boolean showContextMenuForChild(View originalView) { final int longPressPosition = getChildAdapterPosition(originalView); if (longPressPosition >= 0) { final long longPressId = getAdapter().getItemId(longPressPosition); mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, ` longPressId); return super.showContextMenuForChild(originalView); } return false; } public class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo { public RecyclerContextMenuInfo(int position, long id) { this.position = position; this.id = id; } final public int position; final public long id; } } 

现在在你的片段或Activity中实现以下方法。

  @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Inflate Menu from xml resource MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.context_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo(); Toast.makeText(InstanceOfContext , " User selected " + info.position, Toast.LENGTH_LONG).show(); return false; } 

最后在回收站上注册contextMenu

  //for showing a popup on LongClick of items in recycler. registerForContextMenu(recyclerView); 

这应该工作!

如果您想避免在Adapter / ViewHolder中手动定义代码中的菜单,那么您可以使用PopupMenu并从标准的menu.xml资源文件中充入菜单选项。

下面的例子显示了这一点,包括传入一个侦听器的能力,你可以在你的Fragment / Activity中实现对响应上下文菜单的点击。

 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private List<CustomObject> objects; private OnItemSelectedListener listener; private final boolean withContextMenu; class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, PopupMenu.OnMenuItemClickListener { @BindView(R.id.custom_name) TextView name; @BindView(R.id.custom_value) TextView value; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(this); if (withContextMenu) { view.setOnCreateContextMenuListener(this); } } @Override public void onClick(View v) { int position = getAdapterPosition(); if (listener != null) { listener.onCustomerSelected(objects.get(position)); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { PopupMenu popup = new PopupMenu(v.getContext(), v); popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Override public boolean onMenuItemClick(MenuItem item) { if (listener != null) { CustomObject object = objects.get(getAdapterPosition()); listener.onCustomerMenuAction(object, item); } return false; } } public CustomerAdapter(List<CustomObject> objects, OnItemSelectedListener listener, boolean withContextMenu) { this.listener = listener; this.objects = objects; this.withContextMenu = withContextMenu; } public interface OnItemSelectedListener { void onSelected(CustomObject object); void onMenuAction(CustomObject object, MenuItem item); } @Override public CustomerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.snippet_custom_object_line, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { CustomObject object = objects.get(position); holder.name.setText(object.getName()); holder.value.setText(object.getValue()); } @Override public int getItemCount() { return objects.size(); } } 

充分的要点在这里https://gist.github.com/brettwold/45039b7f02ce752ae0d32522a8e2ad9c

您可以将OnCreateContextMenuListener传递给ViewHolder进行绑定。 这个监听器可以为每个数据项创build自定义菜单。 只需在ViewHolder中添加setOnCreateContextMenuListener,并在绑定过程中调用它。

  public static class ItemViewHolder extends RecyclerView.ViewHolder { public ItemViewHolder(View itemView) { super(itemView); } void setOnCreateContextMenuListener(View.OnCreateContextMenuListener listener) { itemView.setOnCreateContextMenuListener(listener); } 

}

在适配器中:

  @Override public void onBindViewHolder(ItemViewHolder viewHolder, int position) { final MyObject myObject = mData.get(position); viewHolder.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (myObject.getMenuVariant() { case MNU_VARIANT_1: menu.add(Menu.NONE, CTX_MNU_1, Menu.NONE,R.string.ctx_menu_item_1); menu.add(Menu.NONE, CTX_MNU_2,Menu.NONE, R.string.ctx_menu_item_2); break; case MNU_VARIANT_2: menu.add(Menu.NONE, CTX_MNU_3,Menu.NONE, R.string.ctx_menu_item_3); break; default: menu.add(Menu.NONE, CTX_MNU_4, Menu.NONE, R.string.ctx_menu_item_4); } } }); } 

在我的情况下,我不得不在onContextItemSelected()方法中使用我的片段中的数据。 我最终要做的解决scheme是将片段的一个实例传递给我的适配器,并将视图项目注册到视图持有者中:

 @Override public void onBindViewHolder(final MyListAdapter.ViewHolder viewHolder, int position) { final Object rowObject = myListItems.get(position); // Do your data binding here viewHolder.itemView.setTag(position); fragment.registerForContextMenu(viewHolder.itemView); } 

然后在onCreateContextMenu()你可以保存索引到一个局部variables:

selectedViewIndex = (int)v.getTag();

并在onContextItemSelected()检索它

好的,根据@ Flexo的回答,我将把mposition命令…

 protected class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { int mPosition; public KWViewHolder(View itemView) { super(itemView); itemView.setOnCreateContextMenuListener(this); } public void setPosition(int position) { mPosition = position; } @Override public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { contextMenu.setHeaderTitle(R.string.menu_title_context); contextMenu.add(0, R.id.menu_delete, mPosition, R.string.delete); } } 

然后在onContextItemSelected我使用

 item.getOrder() 

而且所有的工作都很好,我很容易获得arrays的位置