otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> Android

Title : How to get ExpandableListView long click event for group and child ?
Total Reply : 0
Total View : 1348


Harry
Harry
Total thread: 131
Total reply: 50

Post #172
How to get ExpandableListView long click event for group and child ?
I got a requirement to capture long click event on ExpandableListView group view and child views, there is no setOnGroupLongClickListener or setOnChildLongClickListener, there is only setOnItemLongClickListener, below is the mandatory override to enable this
private class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    // default is FALSE
    // return TRUE if want to get event callback for ExpandableListView.setOnItemLongClickListener(..) to get child id !!!
    return true;
  }

  @Override
  public long getGroupId(int groupPosition) {
    return groupPosition;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
  }

  // ... other overridden methods such as getGroupView, getChildView, getGroupCount, getChildCount, etc..
}
In the ExpandableListView, we set listener by:
mExpandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long packedId) {

    // NOTE: the position is NOT group position, it is the position of the TOTAL OPEN ITEM (groups + childs) until this position !!

    // NOTE: use static ExpandableListView.getPackedPositionType(long packedId) !!!
    int type = ExpandableListView.getPackedPositionType(packedId);

    int groupPosition = ExpandableListView.getPackedPositionGroup(packedId);

    LogHelper.d(TAG, "onItemLongClick(AdapterView, View, position: " + position + ", packedId: " + packedId + "), type: " + type);

    switch (type) {
      case ExpandableListView.PACKED_POSITION_TYPE_GROUP: {
        /*  if group item clicked */
        LogHelper.d(TAG, "long click on groupPosition: " + groupPosition);

        // consume this event and do NOT let Android do more process on it (expand/collapse group)
        return true;
      }
      case ExpandableListView.PACKED_POSITION_TYPE_CHILD: {
        /*  if child item clicked */
        int childPosition = ExpandableListView.getPackedPositionChild(packedId);
        LogHelper.d(TAG, "long click on childPosition: " + childPosition + ", groupPosition: " + groupPosition);

        // consume this event and do NOT let Android do more process on it (expand/collapse group)
        return true;
      }
    }

    return false;
  }
});
I have tried it and able to confirmed working on Nexus 6P (Marshmallow) also on Kitkat
Falling in love with the world

Write : 2016-03-31 12:17:21
Last edit : 2016-03-31 12:37:24

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 712,028

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird