onCreateContextMenu 未被调用
看起来 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需删除
youwidget.setonLongclicklistener
和yourwidget.setLongClickable
然后在
onCreate()
中添加registerforContextmenu(yourwidget)
然后根据使用的小部件添加代码。
希望它会有所帮助。
Just remove
youwidget.setonLongclicklistener
andyourwidget.setLongClickable
And then add
registerforContextmenu(yourwidget)
inonCreate()
then add code according to the widget used.
Hope It will be helpful.
我的问题与 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)
inAdapter.getView(...)
when it should beListView.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.
尝试找到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.
我遇到了这个问题,只能通过确保解决它
我正在考虑放弃长按弹出窗口,转而在卡片的右侧边缘使用 Youtube 风格的按钮,弹出一个菜单,该菜单不会将其他所有内容淡入背景——顺便说一句,该搜索词是什么?
I had that problem and could only resolve it by ensuring
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?
您必须在
onCreate(Bundle savingInstanceState)
中调用registerForContextMenu(View view)
方法。You have to call
registerForContextMenu(View view)
method inonCreate(Bundle savedInstanceState)
.我的想法是 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?