简体   繁体   English

长时间单击listView中的行中的特定项时,获取ContextMenu

[英]Get ContextMenu when long click on specific item in row in listView

I use a base adapter to populate a list view, every row of this list has 3 ImageView . 我使用基本适配器填充列表视图,此列表的每一行都有3 ImageView

I want to show a ContextMenu and get the image position when long click on one of those images(not by click on the row of list, but click on image itself), I tried to inflate the listview row in the Activity and get the image then register this image for contextMenu([registerForContextMenu(imgLeft)]) but nothing works. 我想显示一个ContextMenu并在长按其中一张图像时获得图像位置(不是通过单击列表行,而是单击图像本身),所以我试图在Activity中的listview行充气并获得图像然后将该图像注册为contextMenu([registerForContextMenu(imgLeft)])但没有任何效果。

I thought to call event onLongClickListener on every image in the row but I had a problem which is how I can call(show-open) the contextMenu in the baseAdapter (no Activity here) in method getView ()...? 我本想onLongClickListener中的每个图像上调用事件onLongClickListener ,但是我有一个问题,即如何在方法getView ()中的baseAdapter (此处没有Activity)中调用(显示打开) contextMenu or how I can register those images for contextMenu in the baseAdapter ...? 或如何在baseAdaptercontextMenu注册这些图像...?

I search a lot for solution here and in Google and got nothing. 我在这里和Google中搜索了很多解决方案,却一无所获。

Registering ContextMenu for each ImageView might work. 为每个ImageView注册ContextMenu可能会起作用。 Like this: 像这样:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    if (arg1 == null) {
        LayoutInflater vi = (LayoutInflater) cmx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        arg1 = vi.inflate(R.layout.services_item, null);
    }
    ImageView serviceThumb = (ImageView) arg1.findViewById(R.id.thumbIView);

    registerForContextMenu(serviceThumb);

    serviceThumb.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            menu.setHeaderTitle("Test");
            for (int i = 0; i< 5; i++) {
                menu.add(Menu.NONE, i, i, i + " - Test");
            }
    }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM