在 Android 中按需显示上下文菜单

发布于 2024-10-05 20:42:14 字数 193 浏览 0 评论 0原文

考虑一个带有使用 registerForContextMenu 设置上下文菜单的 Android ListView。当用户长按项目视图时,会显示 ListView 项目的上下文菜单。

此外,我想在用户点击(不是长按)项目时显示上下文菜单(如果满足某些条件)。可以这样做吗?如何?

Consider an Android ListView with a context menu set up with registerForContextMenu. The context menu of a ListView item is shown when the user long taps over the item view.

Additionally, I would like to show the context menu when the user taps (not long taps) on the item (if certain conditions are met). Is it possible to do this? How?

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

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

发布评论

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

评论(2

伤感在游骋 2024-10-12 20:42:14

我通过浏览Android源代码设法解决了这个问题。这就是我所做的:

protected void onItemClick(AdapterView<?> adapterView, int position) {
    final int start = adapterView.getFirstVisiblePosition();
    final int index = position - start;
    final View childView = adapterView.getChildAt(index);
    if (childView != null) {
        adapterView.showContextMenuForChild(childView);
    }
}

I managed to solve this by browsing the Android source code. Here's what I did:

protected void onItemClick(AdapterView<?> adapterView, int position) {
    final int start = adapterView.getFirstVisiblePosition();
    final int index = position - start;
    final View childView = adapterView.getChildAt(index);
    if (childView != null) {
        adapterView.showContextMenuForChild(childView);
    }
}
長街聽風 2024-10-12 20:42:14

是的,从您的点击事件中调用 PerformLongClick 方法,它应该执行此操作

Yes call performLongClick method from your click event and it should do it

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