【问题标题】:Identifying the group that has been clicked in an expandableListView识别已在可展开列表视图中单击的组
【发布时间】:2011-04-27 14:53:09
【问题描述】:

我正在尝试识别已在 expandableListView 中单击的视图。当我设置OnItemLongClickListener 时,我得到一个参数,它显示了单击视图在列表中的位置。但是,它也计算子视图。我希望它只计算组,所以当一个组被点击时,我可以确定它是哪一个。有没有办法做到这一点?

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    不,long 参数不是打包值,这是您的适配器生成的 ID (getCombinedChildId())。尝试解释 ID,即使您以某种方式生成它也是一个坏主意。 id是一个id。

    我相信正确的方法是使用ExpandableListView.getExpandableListPosition(flatPos) 方法。实际上,您传递给侦听器的“pos”参数是平面列表位置。 getExpandableListPosition() 方法返回打包的位置,然后可以使用ExpandableListView 的静态方法将其解码为单独的组和子位置。

    我今天自己也遇到了这个问题,所以我正在描述我发现对我有用的解决方案。

    【讨论】:

    • 只是为了让未来的人更容易,这里有一个例子:ExpandableListView.getPackedPositionGroup(expListView.getExpandableListPosition(position))
    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2017-11-17
      相关资源
      最近更新 更多