在可展开列表视图中识别已点击的组。

4

我正在尝试识别在可扩展列表视图中被点击的视图。当我设置一个OnItemLongClickListener时,我得到了一个参数,它显示了列表内被点击的视图位置。然而,它也计算了子视图。我希望它只计算组,这样当单击组时,我可以确定是哪个组。有什么办法可以做到这一点吗?

2个回答

6
不,长参数不是压缩值,而是由适配器(getCombinedChildId())生成的ID。即使您以某种方式生成ID,试图解释ID也是一个坏主意。 ID就是一个ID。
我认为正确的方法是使用ExpandableListView.getExpandableListPosition(flatPos)方法。传递给侦听器的“pos”参数实际上是平面列表位置。 getExpandableListPosition()方法返回打包的位置,然后可以使用ExpandableListView的静态方法将其解码为单独的组和子位置。
我今天自己遇到了这个问题,所以我正在描述我发现对我有效的解决方案。

为了便于日后的参考,这里有一个例子:ExpandableListView.getPackedPositionGroup(expListView.getExpandableListPosition(position)) - Rinaldi Segecin

2

onItemLongLongClick方法传递的long id参数是一个打包的值。

您可以使用ExpandableListView.getPackedPositionGroup(id)检索组位置。

使用ExpandableListView.getPackedPositionChild(id)获取子项位置。

如果Child == -1,则长按事件发生在组上。

下面是一个示例监听器类,演示了如何解包id

private class expandableListLongClickListener implements AdapterView.OnItemLongClickListener {
    public boolean onItemLongClick (AdapterView<?> p, View v, int pos, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("Long Click Info");
        String msg = "pos="+pos+" id="+id;
        msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id);
        msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id);
        builder.setMessage(msg);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {   }
        } );
        AlertDialog alert = builder.create();
        alert.show();           
        return true;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接