onCreateContextMenu 未被调用

发布于 2024-12-31 21:13:16 字数 578 浏览 2 评论 0原文

看起来 onCreateContextMenu 根本没有被调用。在我的 ListActivity 的 onCreate 中,我有:(

list = getListView();
registerForContextMenu(list);

我知道这是多余的,而且我刚刚传递了 getListView() ,结果相同)。

这是我的 onCreateOntextMenu;

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    Log.d("LM", "onCreateContextMenu");

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_landmarks, menu);
}

日志永远不会生成。难道没有人有什么建议吗?

It looks like the onCreateContextMenu insn't being called at all. In my onCreate for my ListActivity I have:

list = getListView();
registerForContextMenu(list);

(I know it's redundant, and I've just passed getListView() with the same results).

Here is my onCreateOntextMenu;

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    Log.d("LM", "onCreateContextMenu");

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_landmarks, menu);
}

The log never gets generated. Doesn't anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

躲猫猫 2025-01-07 21:13:17

只需删除 youwidget.setonLongclicklisteneryourwidget.setLongClickable

然后在 onCreate() 中添加 registerforContextmenu(yourwidget)
然后根据使用的小部件添加代码。

希望它会有所帮助。

Just remove youwidget.setonLongclicklistener and yourwidget.setLongClickable

And then add registerforContextmenu(yourwidget) in onCreate()
then add code according to the widget used.

Hope It will be helpful.

遮了一弯 2025-01-07 21:13:17

我的问题与 lulumeya 的答案密切相关,它为我指明了正确的方向。我以前做过很多次上下文菜单,但直到现在才遇到过这种情况。

我在 Adapter.getView(...) 中调用 View.setOnClickListener(listener) ,而它应该是 ListView.setOnItemClickListener(listener) 到避免与上下文菜单冲突。

一般来说,我确信 OnItemClickListener 更加优化,特别是因为只使用一个侦听器实例,而不是每次创建或回收视图时都创建一个新实例。

My problem was very closely related to lulumeya's answer, which pointed me in the right direction. I've done context menus many times before and somehow never ran into this until now.

I was calling View.setOnClickListener(listener) in Adapter.getView(...) when it should be ListView.setOnItemClickListener(listener) to avoid conflicting with the context menu.

In general, I'm sure OnItemClickListener is more optimized, especially since only one listener instance is used instead of creating a new instance every time a view is created or recycled.

つ低調成傷 2025-01-07 21:13:17

尝试找到registerForContextMenu(list);作为 onCreate 方法中调用的最后一个方法。
我的意思是这个方法应该在调用列表适配器之后而不是之前调用。

Try to locate registerForContextMenu(list); as ur last method to call in the onCreate method.
I mean this method shoul be called after the list adapter is called not before.

念﹏祤嫣 2025-01-07 21:13:17

我遇到了这个问题,只能通过确保解决它

  1. ,因为 resp。 Activity 注册了视图,然后同一个 Activity 还必须重写 onCreateContextMenu();在片段中执行此操作将不起作用,
  2. 因为我使用了一个额外的片段来创建适配器(并向其注册),片段而不是活动必须重写 onContextItemSelected()。

我正在考虑放弃长按弹出窗口,转而在卡片的右侧边缘使用 Youtube 风格的按钮,弹出一个菜单,该菜单不会将其他所有内容淡入背景——顺便说一句,该搜索词是什么?

I had that problem and could only resolve it by ensuring

  1. that because the resp. Activity registered the Views, the same Activity must then also override onCreateContextMenu(); doing this in a Fragment will not work
  2. since I used an additional Fragment creating the Adapter (and registering with it) the Fragment not the Activity must override onContextItemSelected().

I'm considering abandoning long press popup in favor of a Youtube-style button at the right margin of the cards popping up a menu that does not fade everything else to background---what's the search term for that btw?

离去的眼神 2025-01-07 21:13:17

您必须在onCreate(Bundle savingInstanceState)中调用registerForContextMenu(View view)方法。

You have to call registerForContextMenu(View view) method in onCreate(Bundle savedInstanceState).

宫墨修音 2025-01-07 21:13:16

我的想法是 ListView 拦截事件而不进入 contextMenu 行为。这对我来说很有意义,因为 OnItemLongClickListener 行为与 contextMenu 的行为重叠。如果不是,它如何识别 contextMenu 和 OnItemLongClickListener?

My thought is ListView intercepting the event and not going into contextMenu behaviour. It make sense to me because the OnItemLongClickListener behaviour overlaps contextMenu's. If not how it can recognize between contextMenu and OnItemLongClickListener?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文