Android 在短按时打开 ContextMenu + 通过单击项目的详细信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:31:28  来源:igfitidea点击:

Android open ContextMenu on short click + pass item clicked details

android

提问by Lior Iluz

 lv.setOnItemClickListener(new OnItemClickListener() {
             @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              TextView text = (TextView) view.findViewById(R.id.btitle);
              registerForContextMenu(text);
              view.showContextMenu();
              }
            });
       }

     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
       super.onCreateContextMenu(menu, v, menuInfo);
       TextView text = (TextView) v.findViewById(R.id.btitle);
       CharSequence itemTitle = text.getText();
       menu.setHeaderTitle(itemTitle);

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

     }

Hello,

你好,

I'm trying to open a contextMenu on short item click. I've managed to do so only if I add registerForContextMenu(getListView());somewhere but this also triggers contextMenu on long click (which I don't want to happen).

我正在尝试在短项单击时打开 contextMenu。我只有在添加registerForContextMenu(getListView());某处时才设法这样做, 但这也会在长按时触发 contextMenu(我不希望发生这种情况)。

  • Tried view.showContextMenu()but it doesn't do anything unless I add the registerForContextMenu(getListView());.
  • Tried registering the clicked item first and then call the showContextMenu()but didn't do anything as well...
  • 尝试过view.showContextMenu()但它不会做任何事情,除非我添加registerForContextMenu(getListView());.
  • 尝试先注册点击的项目,然后调用showContextMenu()但也没有做任何事情......

Also, I want to get the clicked item image + text so I can use them in the contextMenu.

另外,我想获得单击的项目图像 + 文本,以便我可以在 contextMenu 中使用它们。

Appreciate the help!

感谢帮助!

采纳答案by Lior Iluz

Solution:

解决方案:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.context_menu, menu);

      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
      long itemID = info.position;
      menu.setHeaderTitle("lior" + itemID);
    }

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;

gives you more details about the list item clicked. Then you can use info.id, info.positionand so on to retrieve the details and use them actions (edit, delete...).

为您提供有关单击的列表项的更多详细信息。然后您可以使用info.id, info.position等来检索详细信息并使用它们操作(编辑、删除...)。

回答by Konrad

I was able to open a context menu on button click with the following code:

我能够使用以下代码在单击按钮时打开上下文菜单:

public void onButtonClickEvent(View sender)
{
    registerForContextMenu(sender); 
    openContextMenu(sender);
    unregisterForContextMenu(sender);
}

Just set the onClick property of the button to onButtonClickEvent. A long click won't trigger the context menu, since it is unregistrered right after it is shown.

只需将按钮的 onClick 属性设置为 onButtonClickEvent。长按不会触发上下文菜单,因为它在显示后立即取消注册。

回答by Kurtis Nusbaum

Regarding opening a context menu on a short click:

关于通过短按打开上下文菜单:

The other solutions posted here didn't work for me because I was using a ListFragment. However the following solution seems to work quite nicely for both a ListFragment and a ListActivity (or just any old listview in general):

此处发布的其他解决方案对我不起作用,因为我使用的是 ListFragment。然而,以下解决方案似乎对 ListFragment 和 ListActivity(或一般的任何旧列表视图)都非常有效:

public void onListItemClick(ListView l, View v, int position, long id){
  l.showContextMenuForChild(v);   
}

It's much more simple and elegant. In fact, this is actually how the ListView class itself initiates the context menu on a long click.

它更加简单和优雅。事实上,这实际上是 ListView 类本身在长按时启动上下文菜单的方式。

回答by Shay Zalman

In my case I had BaseAdapter and implemented click on button for each listView item (in getView method):

在我的例子中,我有 BaseAdapter 并为每个 listView 项目实现了点击按钮(在 getView 方法中):

ImageButton menuBtn = (ImageButton) convertView.findViewById(R.id.itemListMenu);
menuBtn.setOnClickListener(new android.view.View.OnClickListener() {
    public void onClick(View v) {
        parent.showContextMenuForChild(v);
    }
 });

回答by Cheryl Simon

I believe that if you add an OnLongClickListenerto the view, you should be able to prevent it from showing the context menu on long click.

我相信,如果您将OnLongClickListener添加到视图中,您应该能够防止它在长按时显示上下文菜单。

In onItemClick the viewparameter is the item view, so you should be able to get ImageViews or TextViews from that:

在 onItemClick 中,view参数是项目视图,因此您应该能够从中获取 ImageViews 或 TextViews:

view.findById(R.id.my_image_view);

回答by Nicola

My Solution:

我的解决方案:

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {               
            mListView.showContextMenuForChild(view);
        }
    });