【问题标题】:ExpandableListView - onItemLongClick - getPackedPositionChild always returns 0ExpandableListView - onItemLongClick - getPackedPositionChild 总是返回 0
【发布时间】:2013-11-10 21:39:26
【问题描述】:

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

【问题讨论】:

    标签: expandablelistview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 2015-09-27
      • 2014-03-20
      • 2013-04-13
      • 2013-03-30
      • 2016-07-13
      • 2023-04-09
      • 2021-10-27
      • 1970-01-01
      相关资源
      最近更新 更多