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 i开发者_运维百科n
// the ass if/when GOOG fixes this
if( this.hasHeader() && position > 0 ) position--;
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.
精彩评论