ExpandableListView的OnitemLongclickListener事件

expandableListView是带分组的Listview,通常会有setOnChildClickListener,setOnGroupClickListener,但如果是长按的事件,可以用以下方法来实现长按事件的监听

elv.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);
                      Toast.makeText(getApplicationContext(), "childPosition"+childPosition+"groupPosition"+groupPosition, 0).show();
                      //do your per-item callback here
                      return true; //true if we consumed the click, false if not. consume : 消耗

                  } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                      groupPosition = ExpandableListView.getPackedPositionGroup(id);

                      Toast.makeText(getApplicationContext(),"groupPosition"+groupPosition, 0).show();
                      //do your per-group callback here
                      return true; //true if we consumed the click, false if not

                  } else {
                      // null item; we don't consume the click
                      return false;
                  }
          }});
原文地址:https://www.cnblogs.com/yiludugufei/p/4583463.html