onCreateContextMenu isn't being called

2020-07-10 09:33发布

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?

6条回答
Deceive 欺骗
2楼-- · 2020-07-10 09:59

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.

查看更多
够拽才男人
3楼-- · 2020-07-10 09:59

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?

查看更多
男人必须洒脱
4楼-- · 2020-07-10 10:15

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.

查看更多
\"骚年 ilove
5楼-- · 2020-07-10 10:15

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?

查看更多
爷、活的狠高调
6楼-- · 2020-07-10 10:20

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

查看更多
你好瞎i
7楼-- · 2020-07-10 10:22

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.

查看更多
登录 后发表回答