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);
|
阅读(3497) | 评论(1) | 转发(0) |