Chinaunix首页 | 论坛 | 博客
  • 博客访问: 581540
  • 博文数量: 192
  • 博客积分: 3780
  • 博客等级: 中校
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:11
文章存档

2012年(6)

2011年(160)

2010年(26)

分类:

2010-09-07 09:19:23

ExpandableListView长按事件的实现
首先重写onCreateContextMenu方法,具体代码示例如下:
 

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
        int type = ExpandableListView
                .getPackedPositionType(info.packedPosition);
                    if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP )
        {
               Log.i(TAG,"长按事件------在这里写任何长按之后的处理代码");
               menu.add(0,GROUP_C_MENU_DEL,0,"删除" );
               menu.add(0,GROUP_C_MENU_RENAME,0,"重命名" );
        }

       }

然后重写onContextItemSelected方法实现上下文菜单的点击事件的响应,具体代码如下: 

public boolean onContextItemSelected(MenuItem item){
        switch (item.getItemId()) {
         case GROUP_C_MENU_DEL:
                          ExpandableListView.ExpandableListContextMenuInfo info_del =(ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();
             String title_del =((TextView)info_del.targetView).getText().toString();
             
             int groupPos_del = ExpandableListView.getPackedPositionGroup(info_del.packedPosition);
    MSN.msnMessenger.removeGroup(msngroup.get(groupPos_del).getGroupId());
             
             Log.i(TAG, groupPos_del+" ");
             contactsChild.remove(groupPos_del);
             msngroup.remove(groupPos_del);
             myExpandableListAdapter.notifyDataSetChanged();
             MSN.this.makeTip("成功删除一个组", false);
             break;
         case GROUP_C_MENU_RENAME:
             ExpandableListView.ExpandableListContextMenuInfo info_rename =(ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();
             String title_rename =((TextView)info_rename.targetView).getText().toString();
             
             int groupPos_rename = ExpandableListView.getPackedPositionGroup(info_rename.packedPosition);
             renameGroup(groupPos_rename);             
            break;
        }
        return false;
    }

其中获取点击的组和小孩的位置的方法如下:

int group = ExpandableListView
                .getPackedPositionGroup(info.packedPosition);
        int child = ExpandableListView
                .getPackedPositionChild(info.packedPosition);


阅读(3476) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-09 10:45:39

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com