I created an ExpandableListView with the help of this tutorial: link. I understand the code more or less and been trying to set a longclicklistener on the groups.
There is a setOnChildClickListener on the child items already and I managed to set a longclicklistener on them:
exList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
childPosition = ExpandableListView.getPackedPositionChild(id);
//[....]
return false;
}
});
How can I set a longclicklistener on the group items?
I know the code is hard to read so I created a sample project and uploaded it to here. This has no onlongclicklistener on the childs, since this is almost the original from the above link. I would appreciate if someone could help me with this.
Group items are a subset of all items, so the method above should be called in either case. You'd then use getPackedPositionType as above to figure out if the selected item is a group, an item, or null.
The code for this would be:
exList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
int itemType = ExpandableListView.getPackedPositionType(id);
if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
childPosition = ExpandableListView.getPackedPositionChild(id);
groupPosition = ExpandableListView.getPackedPositionGroup(id);
//do your per-item callback here
return retVal; //true if we consumed the click, false if not
} else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
//do your per-group callback here
return retVal; //true if we consumed the click, false if not
} else {
// null item; we don't consume the click
return false;
}
});
If it's a group, you'll use getPackedPositionGroup as above to get the group ID that is being long-pressed. If it's an item, you'll use the combination of getPackedPositionGroup and getPackedPositionChild.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With