Android:当 ListView 包含标题时,上下文菜单回调位置关闭一位

发布于 2024-11-06 12:15:15 字数 773 浏览 0 评论 0原文

我有一个带有标题的 ListView,使用 ListView.addHeader(...) 设置 除非尝试创建上下文菜单,否则它似乎工作得很好。

当用户长按一行时,onCreateContextMenu 回调中的位置似乎会偏移一位。当用户长按标题时,报告的位置是 0 (我不希望回调),当用户长按第一行时,报告的位置是 1 而不是 0 等等。

相同的代码在没有标头的情况下按预期工作。

我知道当我设置标头时,Android 内部只是在我的周围创建一个新的适配器,但我假设它们在调用回调之前处理调整位置。

我看到的是预期的行为吗?也许我做错了什么?

这是我获取位置的方法:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int position = info.position;

    // this is my workaround, but I'm afraid it's going to bite me in
    // the ass if/when GOOG fixes this
    if( this.hasHeader() && position > 0 ) position--;

I have a ListView with a header, set using ListView.addHeader(...)
It appears to work just fine except when trying to create the context menu.

When the user long-presses a row, the position in the onCreateContextMenu callback seems to be off by one. When the user long-presses the header, the position reported is 0 (I wouldn't expect a callback), when the user long-presses the first row, the position reported is 1 instead of 0 and so on.

The same code works as expected without a header.

I know that internally Android just creates a new Adapter around mine when I set a header, but I would assume they handle adjusting the position before invoking callbacks.

Is what I'm seeing expected behavior? Am I doing something wrong, perhaps?

Here's how I fetch the position:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int position = info.position;

    // this is my workaround, but I'm afraid it's going to bite me in
    // the ass if/when GOOG fixes this
    if( this.hasHeader() && position > 0 ) position--;

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-11-13 12:15:15

我知道,当我设置标头时,Android 内部只会在我的周围创建一个新的适配器,但我会假设它们在调用回调之前处理调整位置。

我对此表示怀疑。我还没有尝试过这种特定的行为,但是我自己编写了包装适配器,这不是一个容易解决的问题。

简而言之,上下文菜单内容根本不涉及 Adapter,只涉及 AdapterView。因此,包装适配器没有机会使用菜单事件或调整位置值。

我怀疑您只需要减去 1 并自己忽略标题行。

I know that internally Android just creates a new Adapter around mine when I set a header, but I would assume they handle adjusting the position before invoking callbacks.

I doubt it. I have not tried this specific behavior, but having written wrapping adapters myself, it's not a readily solvable problem.

Simply put, the context menu stuff does not involve the Adapter at all, only the AdapterView. Hence, there is no chance for a wrapping Adapter to either consume menu events or to adjust position values.

I suspect that you will simply need to subtract 1 and ignore the header row yourself.

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