开发者

setonitemclicklistener not working when gridview items use registerForContextMenu

开发者 https://www.devze.com 2023-02-18 22:46 出处:网络
This question has been asked before, mainly with re开发者_JAVA百科ference to ListActivity, and therefore I have not been able to use their solutions. OTOH, I\'m using a normal activity (due to other U

This question has been asked before, mainly with re开发者_JAVA百科ference to ListActivity, and therefore I have not been able to use their solutions. OTOH, I'm using a normal activity (due to other UI elements on it) which contains a gridview. When I use registerForContextMenu() for some of the grid items, the onitemclick() handler is not called for those specific items.

Could someone suggest whether there is a simple solution to this?

I have registered for onItemClickListener() using

assetsListView.setOnItemClickListener(this);

For each of (some of) the ImageView element in the grid, I use

registerForContextMenu(imgView);

The onItemClick() gets called only for those items where the context menu is not registered. Hope this helps.

Thanks, Rajath


I registered each item for onClick, contextMenu and also added a tag that I can use to identify the particular widget for context menu launch.

        registerForContextMenu(imgView);
        imgView.setTag(asset.id);
        imgView.setOnClickListener(this);

When the context menu is launched, i get the tag from the widget for later use:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    if (!(v instanceof ImageView))
        return;

    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, v.getId(), 0, "Launch");
    assetIdViaContextMenu = (String) v.getTag();
}

When the menu item in context menu is clicked, i use the assetIdViaContextMenu to know which widget is being referred to:

@Override
public boolean onContextItemSelected(MenuItem item)
{
    if (assetIdViaContextMenu == null)
        return true;

    Asset asset = getAsset(assetIdViaContextMenu);

    if (item.getTitle() == "Launch")
    {
....

Also, I had to override the cancelation of the context menu.

@Override
public void onContextMenuClosed(Menu menu)
{
    assetIdViaContextMenu = null;
    super.onContextMenuClosed(menu);
}

Hope this helps someone in the future. If you think I could have improved on this, please let me know.

0

精彩评论

暂无评论...
验证码 换一张
取 消