Android LongClickListener on ExpandableListView group items

前端 未结 1 1066
死守一世寂寞
死守一世寂寞 2021-02-03 11:29

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

相关标签:
1条回答
  • 2021-02-03 11:39

    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.

    0 讨论(0)
提交回复
热议问题