如何找出上下文菜单中单击了哪个网格行?

发布于 2024-10-01 03:08:13 字数 91 浏览 1 评论 0原文

我有一个类似于网格的自定义布局。我将其注册为上下文菜单。现在,如果我长按它,我想知道用户单击的位置。我必须让我的自定义布局实现 MenuInfo 对吗?但是点击位置呢?

I've got a custom layout which is like a grid. I registered it for contextmenu. Now if I do a long click on it, I'd like to know the position where the user clicked on. I have to let my custom layout implement MenuInfo right? But what about the click position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-10-08 03:08:13
@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case DELETE_ID:
            AdapterView.AdapterContextMenuInfo info=
                (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

            delete(info.id);
            return(true);
    }

    return(super.onOptionsItemSelected(item));
}

onContextItemSelected() 中,如果上下文菜单适用于 AdapterView(例如 GridView),您可以强制转换 item.getMenuInfo () 对象到 AdapterView.AdapterContextMenuInfo 对象。该对象有一个 id 和一个 position 字段。如果您使用的是 CursorAdapter,则 id_IDposition 是适配器的索引。

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case DELETE_ID:
            AdapterView.AdapterContextMenuInfo info=
                (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

            delete(info.id);
            return(true);
    }

    return(super.onOptionsItemSelected(item));
}

In onContextItemSelected(), if the context menu is for an AdapterView (e.g., GridView), you can cast the item.getMenuInfo() object to an AdapterView.AdapterContextMenuInfo object. That object has an id and a position field. The id is the _ID if you are using a CursorAdapter. The position is the index into your adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文