ExpandableListView - onItemLongClick - getPackedPositionChild 始终返回 0

标签 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;
    }

最佳答案

需要一些附加代码。您必须首先将输入的基于整数的平面列表持仓转换为基于长整型的紧缩持仓。测试代码如下:

@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;
}

关于ExpandableListView - onItemLongClick - getPackedPositionChild 始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895529/

相关文章:

android - 在 onChildClickListener 中使用 Intent 找不到方法

android - ExpandableListView - 如何仅在父元素之间设置分隔符

android - 如何实现Expandablelistview子自定义布局?

java - BaseExpandableListAdapter 的 getChildrenCount() 上出现 NullPointerException

android - 可扩展 ListView 组中的 2 行

Android 用户界面 ListView

android - 带有 ExpandableListView 的 BaseExpandableListAdapter 返回错误的 subview

android - LayoutInflater 和 ExpandableListView

android - 如何在可扩展 ListView 中插入标题行

android - 保存和恢复 ExpandableListActivity 的展开/折叠状态