Last Updated: September 19, 2020
·
6.984K
· bart

ExpandableListView and long click listener done right

Using ExpandableListView with OnItemLongClickListener can be tricky if you need to determine correct child position.

Here is a little snippet to save you some time:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  ExpandableListView listView = (ExpandableListView) parent;
  long pos = listView.getExpandableListPosition(position);

  // get type and correct positions
  int itemType = ExpandableListView.getPackedPositionType(pos);
  int groupPos = ExpandableListView.getPackedPositionGroup(pos);
  int childPos = ExpandableListView.getPackedPositionChild(pos);

  // if child is long-clicked
  if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    Task task = (Task) adapter.getChild(groupPos, childPos);
    ...

Hope that helps :)

1 Response
Add your response

Thank you very much for this.

over 1 year ago ·