Android: long click on the child views of a ExpandableListView?

前端 未结 5 713
星月不相逢
星月不相逢 2020-12-02 06:41

ExpandableListView has a setOnChildClickListener method, but lacks of setOnChildLongClickListener method.

When I added setOnL

5条回答
  •  广开言路
    2020-12-02 07:15

    I was searching for this answer, but non here gave correct results.

    Marked answer from tomash suggest completely different way. Answer from Nicholas is partially correct, but using 'id' is incorrect.

    Correct, working, answer is: convert position parameter to packedPosition and THEN! using this new packedPosition value to obtain group and child ID's. Check code below

        getExpandableListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    
            @Override
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
                long packedPosition = getExpandableListView().getExpandableListPosition(position);
                if (ExpandableListView.getPackedPositionType(packedPosition) == 
                        ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                    // get item ID's
                    int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
                    int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
    
                    // handle data 
                    ...
    
                    // return true as we are handling the event.
                    return true;
                }
                return false;
            }
        });
    

    EDIT: I now see that autobot has almost correct solution, except testing getPackedPositionType on id and not on packetPosition

提交回复
热议问题