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 :)
Written by Bartłomiej Wójtowicz
Related protips
1 Response
 
Thank you very much for this.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Android 
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#

 
 
 
 
