Reputation: 523
I'm trying to identify a view that has been clicked in an expandableListView. When I set an OnItemLongClickListener
I get an argument that shows me the position of the clicked view inside the list. However, it also counts child views. I'd like it to count only groups, so when a group was clicked I can determine which one it was. Is there a way to do that?
Upvotes: 4
Views: 5443
Reputation: 21
The long id
parameter passed by the onItemLongLongClick
method is a packed value.
You can retrieve group position with ExpandableListView.getPackedPositionGroup(id)
Child position is obtained with ExpandableListView.getPackedPositionChild(id)
.
If Child == -1 then the long click was on the group item.
Below is an example listener class demonstrating unpacking of id
.
private class expandableListLongClickListener implements AdapterView.OnItemLongClickListener {
public boolean onItemLongClick (AdapterView<?> p, View v, int pos, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Long Click Info");
String msg = "pos="+pos+" id="+id;
msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id);
msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id);
builder.setMessage(msg);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
} );
AlertDialog alert = builder.create();
alert.show();
return true;
}
}
Upvotes: 2
Reputation: 76
No, the long parameter is not the packed value, this is the ID generated by your adapter (getCombinedChildId()
). Attempting to interpret an ID, even if you generate it in a certain way would be a bad idea. Id is an id.
I believe the right way is to use ExpandableListView.getExpandableListPosition(flatPos)
method. Your "pos" argument passed to the listener is, in fact, flat list position. getExpandableListPosition()
method returns the packed position which can be then decoded into separate group and child positions using the static methods of ExpandableListView
.
I have hit this problem myself today so I am describing the solution I have found working for me.
Upvotes: 6