ExpandableListView - onItemLongClick - getPackedPositionChild始终返回0

jah*_*h15 2 expandablelistview

ExpandableListView.getPackedPositionChild(id)下面总是返回0.小组位置是正确的,但我无法得到正确的孩子位置.下面的代码有什么问题?

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPosition = ExpandableListView.getPackedPositionGroup(id);
            int childPosition = ExpandableListView.getPackedPositionChild(id);
            // do something
            return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

use*_*075 5

需要一些额外的代码.您必须首先将输入的基于整数的平面列表位置转换为基于长的打包位置.测试代码如下:

@Override   
public boolean onItemLongClick( AdapterView<?>    parent,
                                View              view,
                                int               position,
                                long              id) {

    //  convert the input flat list position to a packed position
    long packedPosition = m_expandableListView.getExpandableListPosition(position);

    int itemType        = ExpandableListView.getPackedPositionType(packedPosition); 
    int groupPosition   = ExpandableListView.getPackedPositionGroup(packedPosition);
    int childPosition   = ExpandableListView.getPackedPositionChild(packedPosition);


    //  GROUP-item clicked
    if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        //  ...
        onGroupLongClick(groupPosition);
    }

    //  CHILD-item clicked
    else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        //  ...
        onChildLongClick(groupPosition, childPosition);
    }


    //  return "true" to consume event - "false" to allow default processing
    return false;
}
Run Code Online (Sandbox Code Playgroud)