Android:当 ListView 包含标题时,上下文菜单回调位置关闭一位
我有一个带有标题的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此表示怀疑。我还没有尝试过这种特定的行为,但是我自己编写了包装适配器,这不是一个容易解决的问题。
简而言之,上下文菜单内容根本不涉及
Adapter
,只涉及AdapterView
。因此,包装适配器没有机会使用菜单事件或调整位置值。我怀疑您只需要减去
1
并自己忽略标题行。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 theAdapterView
. Hence, there is no chance for a wrappingAdapter
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.