ExpandableListView.getPackedPositionGroup始终在onItemLongClick中返回0

Ich*_*aum 1 android expandablelistview

对于遇到与我相同问题的人,我会在这里留下:

我试图建立一个OnItemLongClickListenerExpandableListView和想知道的是点击哪个组.按照我在这里提出的许多问题(例如这个问题)的建议,我ExpandableListView.getPackedPositionGroup总是回来0.

这是我使用的代码:

@Override
public boolean onItemLongClick(AdapterView<?> list, View view, int position, long id) {
    Log.d(D, "position: "+position);
    Log.d(D, "id: "+position);
    Log.d(D, "unpacked position: "+ExpandableListView.getPackedPositionGroup(position));

    switch(ExpandableListView.getPackedPositionType(position)) {
        case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
            Log.d(D, "position type: child");
            break;

        case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
            Log.d(D, "position type: group");
            break;

        case ExpandableListView.PACKED_POSITION_TYPE_NULL:
            Log.d(D, "position type: null");
            break;

        default:
            Log.wtf(D, "position type: "+ExpandableListView.getPackedPositionType(position));
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我发现,position和id总是具有相同(正确)的值.但是,类型始终被识别为组,并且解压缩位置始终为0.

那么代码有什么问题呢?

Ich*_*aum 7

发布代码的问题是,这position不是打包位置,而是点击项目的平面位置.这意味着计算组项和可见子项.

解决方案是从平坦的位置获取打包位置,然后解压缩:

final int packedPosition = ((ExpandableListView) list).getExpandableListPosition(position);
final int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
Run Code Online (Sandbox Code Playgroud)

groupPosition 现在保持被点击组的正确位置.